I'm new in Vue and I'm trying to change this.data using a function.
App.vue:
data () {
return {
data: null
}
},
beforeMount: async function () {
function something (response) {
this.data = response
}
something('hello')
console.log(this.data)
}
Error:
Cannot read property 'data' of null
"this" is lost in context.
read This Operator on mozilla
use arrow functions to preserve the scope of this, or bind the function
data () {
return {
data: null
}
},
beforeMount: async function () {
const something = (response) => {
this.data = response
}
something('hello')
console.log(this.data)
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With