<button @click="getNearby" class="nearby">{{ destinationAddress }}</button>
setDestinationAddress (addressComponents) {
this.destinationAddress = addressComponents.city + addressComponents.district + addressComponents.street + addressComponents.streetNumber
},
getLocation (point) {
let that = this
var geoc = new window.BMap.Geocoder()
geoc.getLocation(point, function (rs) {
consoles(rs)
that.setPoint(that.marker, rs.point)
that.setPoint(that.destination, rs.point)
that.setDestinationAddress(rs.addressComponents)
})
},
locationSuccess ({point, addressComponent, marker}) {
consoles(addressComponent)
this.zoom = 16
this.setPoint(this.current, point)
this.setPoint(this.marker, point)
this.setPoint(this.destination, point)
this.setDestinationAddress(addressComponent)
}
locationSuccess是百度地图定位成功回调,变量destinationAddress被getLocation的回调修改,再被locationSuccess修改就报错。
Uncaught TypeError: Cannot read property 'Ha' of null
at Ka.eval (eval at zZ (getscript?v=2.0&ak=aaaaa&services=&t=20180629105709:1), <anonymous>:1:556)
at Ka.x.lang.Ca.dispatchEvent (getscript?v=2.0&ak=aaaaa&services=&t=20180629105709:1)
at Ka.Dd (getscript?v=2.0&ak=aaaaa&services=&t=20180629105709:1)
at Geolocation.eval (eval at zZ (getscript?v=2.0&ak=aaaaa&services=&t=20180629105709:1), <anonymous>:1:1202)
at d (eval at zZ (getscript?v=2.0&ak=aaaaa&services=&t=20180629105709:1), <anonymous>:1:1803)
at eval (eval at zZ (getscript?v=2.0&ak=aaaaa&services=&t=20180629105709:1), <anonymous>:1:2145)
at window.(/anonymous function) (eval at zZ (https://api.map.baidu.com/getscript?v=2.0&ak=aaaaa&services=&t=20180629105709:1:54250), <anonymous>:1:9263)
at ip?qt=loc&coor=bd09ll&ak=aaaaa&timeout=10000&callback=_cbk16835:1
(anonymous) @ VM151757:1
x.lang.Ca.dispatchEvent @ getscript?v=2.0&ak=aaaaa&services=&t=20180629105709:1
Dd @ getscript?v=2.0&ak=aaaaa&services=&t=20180629105709:1
(anonymous) @ VM151757:1
d @ VM151752:1
(anonymous) @ VM151752:1
window.(anonymous function) @ VM151752:1
(anonymous) @ ip?qt=loc&coor=bd09ll&ak=aaaaa&timeout=10000&callback=_cbk16835:1
locationSuccess是百度地图定位成功回调,变量destinationAddress被getLocation的回调修改,再被locationSuccess修改就报错。不知道什么原因
这就属于 js 的闭包了, 这个是在词法作用域决定的那些变量,函数之后可以拿到~
想你定义的两个函数, 一个函数调用另一个函数, 两个函数有自己的函数作用域, 是不能拿到对方的变量的