我目前正在尝试将自己阅读为Angular 4.对于一个项目,我需要在Angular App中使用“HERE Maps”显示地图。 但我不知道如何导入脚本,同时保持访问组件内的类。将第三方JavaScript文件添加到Angular 4
我试图遵循这里的指令: https://developer.here.com/documentation/maps/topics/quick-start.html
我尝试添加的.js文件脚本中的index.html像这样:
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>See720</title>
<base href="/">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="icon" type="image/x-icon" href="favicon.ico">
<script src="http://js.api.here.com/v3/3.0/mapsjs-core.js" type="text/javascript" charset="utf-8"></script>
<script src="http://js.api.here.com/v3/3.0/mapsjs-service.js" type="text/javascript" charset="utf-8"></script>
<script type="text/javascript">
var platform = new H.service.Platform({
'app_id': '****',
'app_code': '****',
useCIT: true
});
</script>
</head>
<body>
<app-root></app-root>
</body>
</html>
我试图以某种方式注入的.js文件转换为组件或服务以便使用.js文件中包含的类。然而我无法让它工作。
我希望把这个部分:
var platform = new H.service.Platform({
'app_id': '****',
'app_code': '****',
useCIT: true
});
...为的OnInit()方法,不知何故。但是“H”对象从未被识别。使用平面旧的JavaScript和Html我可以得到这个工作。
什么是在Angular 4中导入这样一个JavaScript文件的正确方法,以及如何访问它的类和方法?
UPDATE
我的代码目前看起来是这样的: 模板:
<div style="text-align:center">
<!--<div><font color="white">This is some text!</font></div>-->
<div id="mapContainer" style="width: 900px; height: 600px"></div>
</div>
组件:
import {AfterViewInit, Component} from '@angular/core';
declare var H: any;
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent implements AfterViewInit {
title = 'C720';
ngAfterViewInit() {
// Initialize the platform object:
let platform = new H.service.Platform({
'app_id': '****',
'app_code': '****',
useCIT: true
});
// Obtain the default map types from the platform object
let defaultLayers = platform.createDefaultLayers();
// Instantiate (and display) a map object:
let map = new H.Map(
document.getElementById('mapContainer'),
defaultLayers.normal.map,
{
zoom: 5,
center: { lat: 52.5, lng: 13.5 }
}
);
}
}
的index.html:
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>See720</title>
<base href="/">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="icon" type="image/x-icon" href="favicon.ico">
<script src="http://js.api.here.com/v3/3.0/mapsjs-core.js" type="text/javascript" charset="utf-8"></script>
<script src="http://js.api.here.com/v3/3.0/mapsjs-service.js" type="text/javascript" charset="utf-8"></script>
<script src="http://js.api.here.com/v3/3.0/mapsjs-mapevents.js" type="text/javascript" charset="utf-8"></script>
</head>
<body>
<app-root></app-root>
</body>
</html>
地图正在显示,但现在我需要添加一个eventlistener,但第三个包(“mapjs-mapevents.js”)中的H.mapevent类无法识别。
如何告诉Angular查看其他类的第三个脚本?
这已经回答了 - > https://开头计算器.com/questions/37081943/angular2-import-external-js-file-into-component – mutantkeyboard
谢谢你的链接@mutantkeyboard。我更新了上面的问题。我如何从另一个脚本访问同一对象上的类? –
如果您使用的是Angular CLI,请使用'scripts':https://stackoverflow.com/questions/38855891/angular-cli-webpack-how-to-add-or-bundle-external-js-files#答案-39661714 –