capacitor-community / background-geolocation

A Capacitor plugin that sends you geolocation updates, even while the app is in the background.
MIT License
180 stars 56 forks source link

Using variables this. with vueJS #62

Closed benjamin-ret closed 2 years ago

benjamin-ret commented 2 years ago

Describe the bug Hi ! I want to use this.$i18n.t in function callback, but it's undefined ([error] - {})

To Reproduce async mounted () { ... function callback(location, error) { ... console.log(this.$i18n.t('hello') ...

Any idea ? Thanks

Smartphone

diachedelic commented 2 years ago

In JavaScript, this is dynamically bound. This means its value changes depending on how its containing function is called. Here, this does not refer to your component instance. Fortunately, you can save a reference to the component instance and use it later on:

async mounted () { const vm = this; ... function callback(location, error) { ... console.log(vm.$i18n.t('hello') ...