【问题标题】:Vue: Can watchers be used to watch a computed property?Vue:观察者可以用来观察计算属性吗?
【发布时间】:2026-02-06 19:00:02
【问题描述】:

我正在尝试查看是否可以使用观察者方法来查找计算属性中的更改,而不是在此处查看各个商店属性。这会起作用还是只能用于数据属性和道具?

computed: {
      optionsTrue() {
            return this.$store.getters.getOption1IsTrue && this.$store.getters.getOption2IsTrue
      } 
},
watch: {
     optionsTrue(newVal) {
         // would this work to check when the value of the computed property has changed or can watcher only watch data properties?
     }

}

【问题讨论】:

  • 是的,它会起作用的。试试看吧。
  • 是的,观察者可以用来观察计算属性。

标签: vue.js vuejs2


【解决方案1】:

工作代码

computed: {
  completed: function () {
     return this.required && this.textAnswer !== '';
  },
},
watch: {
  completed: function(val) {
    console.log(val)
  }
}

【讨论】:

  • 正如目前所写,您的答案尚不清楚。请edit 添加其他详细信息,以帮助其他人了解这如何解决所提出的问题。你可以找到更多关于如何写好答案的信息in the help center