Use arrow function in vue computed does not work

前端 未结 4 1118
栀梦
栀梦 2020-11-22 07:44

I am learning Vue and facing a problem while using arrow function in computed property.

My original code works fine (See snippet below).

4条回答
  •  隐瞒了意图╮
    2020-11-22 08:10

    You are facing this error because an arrow function wouldn't bind this to the vue instance for which you are defining the computed property. The same would happen if you were to define methods using an arrow function.

    Don’t use arrow functions on an instance property or callback (e.g. vm.$watch('a', newVal => this.myMethod())). As arrow functions are bound to the parent context, this will not be the Vue instance as you’d expect and this.myMethod will be undefined.

    You can read about it here.

提交回复
热议问题