Vue.js vuejs这在我的eventListner中未定义
我尝试在vuejs中制作一个google地图,地图中心的值显示在我的视图中 为此,我创建了我的标记,并尝试使用Vue.js vuejs这在我的eventListner中未定义,vue.js,Vue.js,我尝试在vuejs中制作一个google地图,地图中心的值显示在我的视图中 为此,我创建了我的标记,并尝试使用this.marker=map.center.lng()更新该值或this.setmarkerng(map.center.lng())在我的方法Initmap的事件列表器中,但它们都不起作用。我得到了信息: 这是未定义的 在eventlistener之外,一切都正常 你能帮忙吗 export default { mounted: function () {
this.marker=map.center.lng()更新该值
或this.setmarkerng(map.center.lng())代码>在我的方法Initmap的事件列表器中,但它们都不起作用。我得到了信息:
这是未定义的
在eventlistener之外,一切都正常
你能帮忙吗
export default {
mounted: function () {
this.initMap();
},
data() {
return {
marker: {
lng : 'latitude',
lat: 'longitude'
}
}
},
methods: {
///google map init
initMap() {
var map = new google.maps.Map(document.getElementById('map'), {
center: {lat: 48.587536, lng: 7.751440},
zoom: 13
});
this.setMarkerLat(map.center.lat());
this.setMarkerLng(map.center.lng());
///Listner
autocomplete.addListener('place_changed', function () {
///call method bug
this.setMarkerLat(map.center.lat());
this.setMarkerLng(map.center.lng());
infowindow.close();
this.marker.lat = map.center.lat();
this.marker.lng = map.center.lng();
marker.setVisible(false);
var place = autocomplete.getPlace();
if (!place.geometry) {
window.alert("No details available for input: '" + place.name + "'");
return;
}
// If the place has a geometry, then present it on a map.
if (place.geometry.viewport) {
map.fitBounds(place.geometry.viewport);
} else {
map.setCenter(place.geometry.location);
map.setZoom(17); // Why 17? Because it looks good.
}
marker.setPosition(place.geometry.location);
marker.setVisible(true);
var address = '';
if (place.address_components) {
address = [
(place.address_components[0] && place.address_components[0].short_name || ''),
(place.address_components[1] && place.address_components[1].short_name || ''),
(place.address_components[2] && place.address_components[2].short_name || '')
].join(' ');
}
},
setMarkerLng: function(lng) {
this.marker.lng = lng;
},
setMarkerLat: function(lat){
this.marker.lat = lat;
}
}
}
尝试使用箭头功能()=>
,以便能够访问此范围,如:
autocomplete.addListener('place_changed', ()=> {
...
}
尝试使用箭头功能()=>
,以便能够访问此范围,如:
autocomplete.addListener('place_changed', ()=> {
...
}
非常感谢!!因此,如果我想确定vue对象的范围,我需要使用arrow?是的,或者您应该将此
分配给一个全局变量,并在回调函数中访问该变量谢谢!!因此,如果我想确定vue对象的范围,我需要使用arrow?是的,或者您应该将此
分配给一个全局变量,并在回调函数中访问该变量尝试将自动完成
值绑定到某个属性,如位置
,然后观看这个道具并分派你放在place\u changed
事件中的函数。将place\u changed
事件中执行的代码移动到“placeprop
更改”时将调用的方法。尝试将autocomplete
值绑定到某个prop,如place
,然后观看此prop并分派放置在place\u changed
事件中的函数。将place\u changed
事件中执行的代码移动到一个方法中,该方法将在`placeprop
change'时调用。