地图选点组件,第一次选完没问题,返回上个页面后再进入,就获取不了数据了,是什么原因?
<template>
<div class="map-warp">
<web-view
src="https://apis.map.qq.com/tools/locpicker?search=1&type=1&key=我的key&referer=esx_ydxt"
@message="onMessage">
</web-view>
</div>
</template>
<script>
export default {
data() {
return {
height: uni.getSystemInfoSync().screenHeight,
map:[]
}
},
onUnload(){
//关闭页面把数据存入缓存
console.log('存入缓存')
getApp().globalData.usermap=this.map
console.log(getApp().globalData.usermap)
chooseLocation.setLocation(null);
},
onLoad() {
getApp().globalData.mapstatus=1
getApp().globalData.usermap=[]
},
onReady() {
const self = this
if (window.isListen) {
// 防止多次注册addEventListener事件
return
}
window.addEventListener('message', function (event) {
var loc = event.data;
if (loc && loc.module == 'locationPicker') {
console.log('地图数据', loc);
self.map=loc
self.getPositon(loc, self)
console.log('地图数据', self.map);
};
window.isListen = true
window.removeEventListener('message', function() {}, true)
}, false)
},
methods: {
onMessage(res) {
console.log('app接收网页消息:', res.detail.data[0])
this.getPositon(res.detail.data[0], this)
},
getPositon(res, self) {
uni.$emit('onAddressChange', res)
setTimeout(function() {
self.$nav.back()
}, 1000)
}
},
}
</script>
<style scoped lang="scss">
</style>