computed vs watch

217 阅读1分钟

简易理解 fullname 结果来自于first last get中first last --->其一改变 fullname 改变 set中 fullname改变 ---->first last 随之得到结果

computed: {
  fullName: {
    // getter
    get: function () {
      return this.firstName + ' ' + this.lastName
    },
    // setter
    set: function (newValue) {
      var names = newValue.split(' ')
      this.firstName = names[0]
      this.lastName = names[names.length - 1]
    }
  }
}

如果 question 发生改变,这个函数就会运行

watch: {
    // 如果 `question` 发生改变,这个函数就会运行
    question: function (newQuestion, oldQuestion) {
      this.answer = 'Waiting for you to stop typing...'
      this.debouncedGetAnswer()
    }
  },