JS - 地理定位功能错误,但一切正常
JS - geolocation function error , while everything works OK
我正在做天气应用程序,一旦单击 'find me!' 按钮,它就会激活 getCoords 功能:
getCoords(e){
e.preventDefault();
if(navigator.geolocation){
navigator.geolocation.getCurrentPosition((position) =>{
const long = position.coords.longitude,
lat = position.coords.latitude;
console.log(`long ${long} and lat ${lat}`);
this.setMessage('http://api.openweathermap.org/data/2.5/forecast?lat='+lat+'&lon='+long+"&APPID=mykey");
}).bind(null,this);
}
}
因为我对结果感到满意,它给出了结果并且它确实像我期望的那样工作,我在控制台中遇到了很多错误,这让我感到有点担心。
火狐浏览器:
TypeError: navigator.geolocation.getCurrentPosition(...) is undefined
chrome 和歌剧:
Uncaught TypeError: Cannot read property 'bind' of undefined
at WeatherApp.getCoords (app.bundle.js?49cb1f92ba06aee2ea42:374)
歌剧:
Failed to load resource: the server responded with a status of 404 (Not Found)
您没有将 'this' 绑定到 tu 函数,而是它的 return 值。
'bind'是Function原型的一个方法。我不确定您为什么要使用绑定调用。只需在调用函数之前删除它或调用它。
我正在做天气应用程序,一旦单击 'find me!' 按钮,它就会激活 getCoords 功能:
getCoords(e){
e.preventDefault();
if(navigator.geolocation){
navigator.geolocation.getCurrentPosition((position) =>{
const long = position.coords.longitude,
lat = position.coords.latitude;
console.log(`long ${long} and lat ${lat}`);
this.setMessage('http://api.openweathermap.org/data/2.5/forecast?lat='+lat+'&lon='+long+"&APPID=mykey");
}).bind(null,this);
}
}
因为我对结果感到满意,它给出了结果并且它确实像我期望的那样工作,我在控制台中遇到了很多错误,这让我感到有点担心。 火狐浏览器:
TypeError: navigator.geolocation.getCurrentPosition(...) is undefined
chrome 和歌剧:
Uncaught TypeError: Cannot read property 'bind' of undefined at WeatherApp.getCoords (app.bundle.js?49cb1f92ba06aee2ea42:374)
歌剧:
Failed to load resource: the server responded with a status of 404 (Not Found)
您没有将 'this' 绑定到 tu 函数,而是它的 return 值。
'bind'是Function原型的一个方法。我不确定您为什么要使用绑定调用。只需在调用函数之前删除它或调用它。