[英]How does Vue.js set observers in computed properties?
If I have a computed property that looks like this: 如果我有一个看起来像这样的计算属性:
computed: {
greeting() {
const state = this.$store.state;
if (state.name === 'Joe') {
return 'Hey, Joe';
} else {
return 'Hello, ' + state.name;
}
}
}
What object(s) is Vue going to set an observer on? Vue将在哪些对象上设置观察者?
this.$store.state
or state.name
or both? this.$store.state
或state.name
还是两者? Asking because: 询问原因:
Provided that the this.$store.state
and this.$store.state.name
properties are reactive (they should be), then Vue will observe changes to these properties and re-evaluate greeting
when the values of these properties change. 假设
this.$store.state
和this.$store.state.name
属性是反应性的(应该是反应性的),则Vue将观察到这些属性的更改,并在这些属性的值更改时重新评估greeting
。
No other properties will be observed. 不会观察到其他属性。
This will cause greeting
to be re-evaluated: 这将导致
greeting
被重新评估:
this.$store.state.name = 'foo';
this.$store.state = bar();
This will not cause greeting
to be re-evaluated: 这不会导致
greeting
被重新评估:
this.$store.state.foo = 'foo';
this.$store.state.a.b.c = 'bar';
this.$store.apple = 'pink lady';
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.