观察计算属性的代码似乎从未运行。

huangapple go评论64阅读模式
英文:

Code for watched computed property never seems to run

问题

我正在观察一个返回状态变量的计算属性。

我确定这个状态变量会因为我做的某些事情而改变。然而,在我的watch: {}中的console.log()代码从未执行:

computed: {
  simulation () {
    return this.$store.state.simulation
  }
},
watch: {
  simulation () {
    console.log('simulation changed:')
  }
}

我做错了什么?

英文:

I'm watching a computed property returning a state variable.

I know for sure that this state variable changes as a result of certain things I do. Yet the console.log() code in my watch: {} never executes:

computed: {
  simulation () {
    return this.$store.state.simulation
  }
},
watch: {
  simulation () {
    console.log('simulation changed:')
  }
}

What am I doing wrong?

答案1

得分: 0

通过我们在原始帖子评论中的对话,我们发现simulation的一个属性正在发生变化,而不是整个simulation对象。要检测这种变化,楼主只需要watch正在变化的simulation特定属性,而不是整个simulation对象。

英文:

Through our conversation in the comments of the original post, we found that a property of simulation was being changed, rather than the entire simulation object. To detect such changes, OP simply needs to watch the specific property of simulation that is being changed, rather than the whole simulation object.

答案2

得分: 0

如果您的 this.$store.state.simulation 是对象,您无法监视对象属性的更改。您可以监视对象的更改(当 this.$store.state.simulation = newVal 时,但不是 this.$store.state.simulation.anyProp = newVal)您应该使用深度监视(https://v2.vuejs.org/v2/api/#watch)

英文:

If your this.$store.state.simulation is object, you can't watch for change property of object. You can watch change of object (when this.$store.state.simulation = newVal, but not this.$store.state.simulation.anyProp = newVal)
you should use deep watch (https://v2.vuejs.org/v2/api/#watch)

huangapple
  • 本文由 发表于 2020年1月4日 00:56:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/59582397.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定