计算属性是类似于带缓存的属性property函数,不过决定是否重新计算得到属性值的传入参数是隐含的使用到的this的数据。

例:

  computed: {
    // 计算属性的 getter
    publishedBooksMessage() {
      // `this` 指向 vm 实例
      return this.author.books.length > 0 ? 'Yes' : 'No'
    }
  }

publishedBooksMessage就是计算属性,其是否重新进行计算则依赖于this.author.books.length是否发生了变化。

当需要执行开销较大的异步操作时,我们则需要使用到watch方法