返回值未定义
Value returning as undefined
我需要获取我的纬度和经度。在我的函数中,它 returns 非常好,但是如果我尝试调用相同的 this.latitude 但功能失效,它 returns 为未定义。
localizar() {
this.geolocation.getCurrentPosition().then((position) => {
this.latitude = position.coords.latitude;
this.longitude = position.coords.longitude;
// Here it returns
console.log('teste: ', this.latitude, this.longitude);
})
// Here returns as undefined
console.log('lat: ', this.latitude, 'lng: ', this.longitude)
}
您可以使用 async/await 个功能。
async localizar() {
const position = await this.geolocation.getCurrentPosition();
this.latitude = position.coords.latitude;
this.longitude = position.coords.longitude;
// your logic here
}
有关 async functions and Promises 的更多信息:
async localizar() {
const position = await this.geolocation.getCurrentPosition();
this.latitude = position.coords.latitude;
this.longitude = position.coords.longitude;
// your logic here
}
我需要获取我的纬度和经度。在我的函数中,它 returns 非常好,但是如果我尝试调用相同的 this.latitude 但功能失效,它 returns 为未定义。
localizar() {
this.geolocation.getCurrentPosition().then((position) => {
this.latitude = position.coords.latitude;
this.longitude = position.coords.longitude;
// Here it returns
console.log('teste: ', this.latitude, this.longitude);
})
// Here returns as undefined
console.log('lat: ', this.latitude, 'lng: ', this.longitude)
}
您可以使用 async/await 个功能。
async localizar() {
const position = await this.geolocation.getCurrentPosition();
this.latitude = position.coords.latitude;
this.longitude = position.coords.longitude;
// your logic here
}
有关 async functions and Promises 的更多信息:
async localizar() {
const position = await this.geolocation.getCurrentPosition();
this.latitude = position.coords.latitude;
this.longitude = position.coords.longitude;
// your logic here
}