得到这是空的

Pra*_*avu 3 typescript angular

我收到了一些奇怪的错误“这是空的”。我不知道是什么问题。这是我的演示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);
  }
Run Code Online (Sandbox Code Playgroud)

pza*_*ger 5

由于所需的范围,您需要使用箭头函数(() => {而不是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)
})
Run Code Online (Sandbox Code Playgroud)

也许值得一读:使用箭头函数或箭头函数和“this”关键字理解 JavaScript 中的“this”