0

So, I'm developing a Cordova Android app with VueJS and I use background geolocation plugin

It emits global event called 'location' which my app listens in main.js

function onDeviceReady () {
  BackgroundGeolocation.on('location', (location) => {})
}

document.addEventListener('deviceready', onDeviceReady, false)

How can I pass location data to variable in component every time event is fired?

1 Answer 1

2

Try to add event listener in mounted method in your component and point it's handler to component method, like this:

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
        }
    }
})
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you soooo much, you really saved me a lot of time! I can only add that I added window.BackgroundGeolocation.on() to make it work.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.