gpt4 book ai didi

angular - 知道这是空的

转载 作者:行者123 更新时间:2023-12-03 12:25:49 24 4
gpt4 key购买 nike

我收到了一些奇怪的错误“这是空的”。我不知道问题是什么。这是我的演示 Stackblitz.com和示例代码作为您的引用。

组件

ngOnInit() {
this.getCurrentLocation();
}

getCurrentLocation() {
navigator.geolocation.getCurrentPosition(position =>{
this.lat = position.coords.latitude;
this.lng = position.coords.longitude;
this.weatherInfo(this.lat, this.lng)
},function(){
this.origin = { lat: '3.140853', lng: '2.693207'};
console.log(this.origin)
this.weatherInfo(this.origin.lat, this.origin.lng)
})



}
weatherInfo(lat, lng){
console.log(lat, lng);
}

最佳答案

由于所需的范围,您需要使用箭头函数(() => { 而不是 function() {):

getCurrentLocation() {
navigator.geolocation.getCurrentPosition(position =>{
this.lat = position.coords.latitude;
this.lng = position.coords.longitude;
this.weatherInfo(this.lat, this.lng)
}, () => { // Here
this.origin = { lat: '3.140853', lng: '2.693207'};
console.log(this.origin)
this.weatherInfo(this.origin.lat, this.origin.lng)
})

也许值得一读:Understanding "this" in javascript with arrow functionsArrow functions and the ‘this’ keyword

关于angular - 知道这是空的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60504760/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com