Using curly braces {
in arrow functions starts a block, not an object.
To make it work properly:
data: () => ({
})
Make sure to include the parentheses (
and )
. As specified in MDN/Arrow Functions/Syntax:
Syntax - Advanced Syntax
// Enclose the function body in parentheses to return an object literal expression:
params => ({foo: bar})
In Vue, avoid using arrow functions with API Docs:
Do not use an arrow function for the data
property as it binds the parent context. Using this
may not refer to the Vue instance and could lead to unexpected behavior.
Update:
> **Response:** Even with the recommended way, you can't use `this`. What is the purpose then?
It is possible. For modifying a prop's value (with v-model
), it is advised to create an internal property like internalStuff
within data
and initialize it with the props value:
Vue.component('my-component', {
props: ['stuff'],
data() {
return {internalStuff: this.stuff}; // Works fine without arrow functions
},
template: `<input type="text" v-model="internalStuff">`
}