0
嘿家伙我想知道如何从2个方法中访问全局范围(this)中的变量。帮助赞赏从方法Typescript访问全局变量
location: any;
doSomethingOne() {
Geolocation.getCurrentPosition().then((resp) => {
this.location = resp;
this.doSomething();
}).catch((error) => {
console.log('Error getting location', error);
});
}
doSomething() {
return new Promise(resolve => {
this.http.get('http://xxxxxxxxx.xxxx/api/' + this.location.coords.latitude + '/' + this.location.coords.longitude + '/5')
.map(res => res.json())
.subscribe(data => {
this.data = data;
resolve(this.data);
});
});
}
如何从Promise中访问this.location?控制台显示 '未定义' 访问它
'this'不是全局作用域时。 '窗户'是。如果你想获得* object *作用域,箭头函数作为'then'回调应该这样做。 – estus