javascript - 无法读取 null bing map 和 Angular4 的属性 'prototype'

标签 javascript angular typescript bing-maps

我正在尝试在 Angular 4 中制作 bing 组件,但它根本不允许我渲染 map ..

这是我的index.html 文件:

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>Microsoft</title>
  <base href="/">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="icon" type="image/x-icon" href="favicon.ico">
  <script type='text/javascript' src='http://www.bing.com/api/maps/mapcontrol?branch=experime‌​ntal'></script>
</head>
<body onload="AppComponent.helloWorld()">
<div id="myMap"></div>
  <app-root></app-root>
</body>
</html>

这是我的组件:

 export class AppComponent implements OnInit {
  @ViewChild('myMap') myMap;
  title = 'app';

  ngOnInit() {
    if(Microsoft !== undefined){
    var map = new Microsoft.Maps.Map(this.myMap.nativeElement, {
      credentials: 'key goes here'
    });
  }}
}

组件 HTML 文件:

<div #myMap style='width: 100%; height: 500px;'></div>

我不知道我做错了什么。但我无法加载 map 。它抛出无法读取空错误的属性“原型(prototype)”。

最佳答案

执行ngOnInit()时,#myMap不存在,因此将代码移至ngAfterViewChecked()

关于javascript - 无法读取 null bing map 和 Angular4 的属性 'prototype',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45825732/

相关文章:

javascript - 参数 'AppCtrl' 不是一个函数,未定义,似乎无法修复

Angular 6 providedin 不起作用 -> StaticInjectorError

json - 将 res.json() 转换为 Array<Object>

json - 如何使用Observable从文件中获取json数据

angular - Ionic 4 无法导入自定义组件不是已知元素

javascript - 无法从 View 访问 Controller 内部的功能

两个标签之间文本的 Javascript If 语句

javascript - 在 javascript 中使用关联数组

html - 如何在 Angular4 中添加由某些用户输入定义的自定义 CSS?

javascript - 从 react-router-dom 属性 'sumParams' 升级版本 4 useParams () 后 TypeScript 错误在类型 '{}' 上不存在