英文:
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)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论