Notes on calling data object in Vue methods
Updated at
2024.3.9
Created at
2020.8.31
When you want to call a data
object in a method defined on a Vue (Nuxt.js) instance, the common notation will not work.
Correct definition of methods
sample.js
export default {
data() {
return {
message: 'test',
}
},
methods: {
update: function () {
this.message = 'updated'
},
wrong: () => {
this.message = "can't access"
},
},
}
If you write a method with an arrow function, you will not be able to access the data
object (blind spot...) ...
I think this happens because this
is interpreted differently in JS function
and in Arrow functions.
If you follow the documentation and define the function without abbreviations, you can call it.