Vue JS: Difference of data() { return {} } vs data:() => ({ })

前端 未结 2 1307
时光取名叫无心
时光取名叫无心 2020-11-28 11:23

I\'m curious both of this data function, is there any difference between this two.

I usually saw is

data () {
  return {
    obj
  }
}
2条回答
  •  情书的邮戳
    2020-11-28 11:34

    It has something to do with ES5 or ES6 syntax, If you have used arrow functions ()=> in your previous scripts it is safe to use the following codes.

    // tested and this.myData can be accessed in the component
    data: () => { return {
        myData: 'someData',
        myStuff: this.stuffProp
    } }
    
    // this also works
    data: () => ({
        myData: 'someData',
        myStuff: this.stuffProp
    })

提交回复
热议问题