javascript - JavaScript ES6 返回 undefined 虽然已设置
问题描述
我有一门课来处理地理信息和谷歌地图服务:
class MapService {
constructor(apiKey) {
this.apiKey = apiKey;
this.googleMapsClient = require('@google/maps').createClient({
key: this.apiKey
});
}
getLocationFromGeoData(){
this.coords = this.getCoordinatesFromGeoData();
console.log(this.coords); // => undefined
return this.coords;
}
getCoordinatesFromGeoData(){
if(navigator.geolocation){
navigator.geolocation.getCurrentPosition(function(position){
let coords = {
lat: position.coords.latitude,
lng: position.coords.longitude
};
console.log(coords); // shows object with coordinates
return coords;
})
} else {
console.warn("Geolocation wird nicht unterstützt!");
}
}
}
export {MapService}
我的导入:
import {MapService} from './modules/MapServices';
let googleMapsApiKey = document.head.querySelector('meta[name="google-maps-api-key"]');
window.MapService = new MapService(googleMapsApiKey.content);
现在在我的脚本(Vue.js)中,我称之为类方法getLocationFromGeoData
:
var coords = MapService.getLocationFromGeoData();
console.log(coords); // => undefined
除了当getCoordinatesFromGeoData
我undefined
尝试访问我的坐标对象时得到的。
从导入的类返回值时,我是否需要处理一些特殊的事情?
解决方案
推荐阅读
- java - Java Geolocation 计算 - 没有得到正确的值
- python - SymbolicTransformer 生成的新特征不符合规则?
- python - 使用 selenium 单击多个链接
- jquery - 如何将事件绑定到jQuery中的所有输入字段
- go - 在未导出的字段上调用导出的方法
- mysql - 寻找有关如何使用随机 PII 数据生成数据库的指导
- c# - 我想下载我在用户系统上使用 XmlWriter.Create() 创建的 C# 中的 XML 文件
- sql - Rstudio 错误:输入“”不匹配。期待:
- google-cloud-functions - Google Functions + 在实例上执行 bash 脚本
- c# - Angular 8 和 C# 控制器