调用函数functionX时出错:ERROR TypeError:无法读取null的属性'functionX'

时间:2018-03-29 19:09:42

标签: angular typescript ionic3

我正在使用Ionic3,我正在尝试将geolocation cordova源代码集成到我的文件about.ts中,我收到一个奇怪的错误:

当我从html调用getMapLocation()时:

(click)="getMapLocation()"

结果如下:

core.js:1448 ERROR TypeError: Cannot read property 'functionX' of null
    at webpackJsonp.195.AboutPage.onMapSuccess (about.ts:94)
    at t.invoke (polyfills.js:3)
    at Object.onInvoke (core.js:4749)
    at t.invoke (polyfills.js:3)
    at r.runGuarded (polyfills.js:3)
    at polyfills.js:3

为什么我不能打电话给“functionX”?

about.ts:

//Imports + Decorators
export class AboutPage{
//Constructor + some code

  getMapLocation() {
    navigator.geolocation.getCurrentPosition(this.onMapSuccess, this.onMapError, { enableHighAccuracy: true });

  }

  onMapSuccess(position) {
    this.functionX();
    console.log(position);
    let Latitude = position.coords.latitude;
    console.log('sss');
    let Longitude = position.coords.longitude;
    console.log('11');
    this.getMap(Latitude, Longitude);
    console.log('22');
  }  
  functionX(){
    console.log('functionX');
  }

1 个答案:

答案 0 :(得分:0)

上下文不正确,将函数绑定或传递给前两个getCurrentPosition参数:

navigator.geolocation.getCurrentPosition(this.onMapSuccess.bind(this), this.onMapError.bind(this), { enableHighAccuracy: true });

navigator.geolocation.getCurrentPosition(() => this.onMapSuccess(), () => this.onMapError(), { enableHighAccuracy: true });