*map.js
import AMap from 'AMap'
export function add(map, overlayers) {
console.log(map)
map.add(overlayers)
}
*xxx.vue
这边引入map.js中的add函数
import { mapInit, addMarker, setLabel, add } from 'common/js/map'
假如我把地图实例赋值给 this.map2 那边add函数输出的 map 为undefind
this.map2 = mapInit(document.getElementById('mapcontent2'));
this.map2.on('click', function(e) {
marker = addMarker({
position:[e.lnglat.getLng(),e.lnglat.getLat()],draggable:true,cursor:'move'});
add(this.map2,[marker])
});
如果赋值给单独的 let map1,map2; 那边add函数输出的 map 就可以
这个是什么问题呢?
还望知情人士 对我进行 爱的教导!
你使用的回调函数
element.on('click',callback)
。callback里的this指向的不是Vue实例。 你可以在
dialogShow
方法的开头const that=this
,然后使用that.map2。