因此,我正在使用VueJS开发Cordova Android应用,并且我使用background geolocation plugin
它发出一个名为“位置”的全局事件,我的应用程序在main.js中监听
function onDeviceReady () {
BackgroundGeolocation.on('location', (location) => {})
}
document.addEventListener('deviceready', onDeviceReady, false)
每次触发事件时如何将位置数据传递给组件中的变量?
答案 0 :(得分:1)
尝试在组件的mounted
方法中添加事件侦听器,并将其处理程序指向组件方法,如下所示:
export default {
data() {
return {
location: null,
}
},
mounted() {
document.addEventListener('deviceready', this.onDeviceReady, false)
},
beforeDestroy() {
// remember to remove event listener
document.removeEventListener('deviceready', this.onDeviceReady)
},
methods: {
onDeviceReady() {
BackgroundGeolocation.on('location', this.handleLocation)
},
handleLocation(location) {
// you've got location!
this.location = location
}
}
})