javascript - ionic 3 : Cannot read property 'nativeElement' of undefined TypeError: Cannot read property 'nativeElement' of undefined

标签 javascript google-maps typescript geolocation ionic3

我正在研究 IONIC 3,现在我正在使用 Google Maps API 开发一个应用程序。问题是我正在尝试执行我的应用程序,但是当它启动时,会出现此消息:

inicio.html

Error: Uncaught (in promise): TypeError: Cannot read property 
'nativeElement' of undefined
TypeError: Cannot read property 'nativeElement' of undefined

这是产生问题的代码片段:

inicio.ts(只是部分代码)

loadMap(){

this.geolocation.getCurrentPosition().then((position)=>{

  let latLng = new google.maps.LatLng(position.coords.latitude,
                                      position.coords.longitude);

  let mapOptions = {
    center: latLng,
    zoom: 15,
    mapTypeId: google.maps.MapTypeId.ROADMAP
  }

  this.map = new google.maps.Map(this.mapElement.nativeElement, 
  mapOptions);

  }, (err) => {
  console.log(err);
  });

}

inicio.ts(完整代码)

import { Component,ViewChild, ElementRef } from '@angular/core';
import { IonicPage, NavController, NavParams } from 'ionic-angular';
import { Geolocation } from '@ionic-native/geolocation';
import { Platform } from 'ionic-angular';

declare var google: any;

/**
* Generated class for the InicioPage page.
*
* See https://ionicframework.com/docs/components/#navigation for more 
info on
* Ionic pages and navigation.
*/

@IonicPage()
@Component({
selector: 'page-inicio',
templateUrl: 'inicio.html',
})
export class InicioPage {

@ViewChild('map') mapElement: ElementRef;
map: any; //Google map manager.
coords : any = { lat:0, lng: 0 } 

constructor(
public navCtrl: NavController, 
public navParams: NavParams,
public platform: Platform,
public geolocation: Geolocation) {

  platform.ready().then(() => {
      //Platform ready. All plugins enabled.
  });
}

ionViewDidLoad() {
console.log('ionViewDidLoad InicioPage');
this.loadMap();
}

loadMap(){

this.geolocation.getCurrentPosition().then((position)=>{

  let latLng = new google.maps.LatLng(position.coords.latitude,
                                      position.coords.longitude);

  let mapOptions = {
    center: latLng,
    zoom: 15,
    mapTypeId: google.maps.MapTypeId.ROADMAP
  }

this.map = new google.maps.Map(this.mapElement.nativeElement, 
mapOptions);

}, (err) => {
  console.log(err);
});

}
}

inicio.html

<!--
Generated template for the InicioPage page.

See http://ionicframework.com/docs/components/#navigation for more 
info on
Ionic pages and navigation.
-->
<ion-header>

<ion-navbar>
<ion-title>Inicio</ion-title>
</ion-navbar>

</ion-header>


<ion-content padding>
<!--<h1> Esta es la página de inicio, aquí situaremos un mapa con un 
boton de añadir lugar.</h1>-->
<div id="map"></div>

</ion-content>

inicio.scss

page-inicio {

ion-content{
    .scroll{
        height: 100%;   
    }
    #map{
        width: 100%;
        height: 100%;
    }
}

}

感谢您的帮助!

最佳答案

在您的 HTML 中您需要更改

<div id="map"></div>

<div #map id="map"></div>

然后 @ViewChild('map') mapElement: ElementRef; 将正确解析。

参见 this了解更多详情。

关于javascript - ionic 3 : Cannot read property 'nativeElement' of undefined TypeError: Cannot read property 'nativeElement' of undefined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46269121/

相关文章:

javascript - 你应该将 IIFE 与 RequireJS 一起使用吗

android - 谷歌地图 android "android Failed to load map. Could not contact Google servers."中的错误

mysql - 使用 Mysql 使用 setPanel() 显示文本方向

iphone - 在 Xcode 中打开原生谷歌地图

angular - 在带有模板的 Angular ng-bootstrap 模式中,关闭和关闭按钮不起作用

reactjs - 为什么 React.FC 不允许我只返回 child ?

javascript - 如何更新网页的一部分而不重定向到新页面? Express、NodeJS、 Mongoose

javascript - 如何使用过滤器返回一个新数组减去已选择的一项?

javascript - 如何在 ASP.NET 中从 Facebook 登录检索访问 token ?

TypeScript 条件类型和 typeof