我正在使用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”?
//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');
}
答案 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 });