I'm curious both of this data function, is there any difference between this two.
I usually saw is
data () {
return {
obj
}
}
And ES6 "fat arrow" which I typically used
data:()=>({
obj
})
I'm curious both of this data function, is there any difference between this two.
I usually saw is
data () {
return {
obj
}
}
And ES6 "fat arrow" which I typically used
data:()=>({
obj
})
No difference in your specific example, but there is a very important difference between those two notations, specially when it comes to Vue.js: the
this
won't reflect the vue instance in arrow functions.So if you ever have something like:
It won't work as you expect. The
this.stuffProp
won't get thestuffProp
prop's value (see below for more on the reason why).Fix
Change the arrow function to, either (ES6/EcmaScript 2015 notation):
Or to (regular, ES5 and before, notation):
Reason
Don't use arrow functions (
() => {}
) when declaring Vue methods. They pick up thethis
from the current scope (possiblywindow
), and will not reflect the Vue instance.From the API Docs: