1. Modify the state in Vuex on page A. Need to monitor in real time on page B.
2. How to achieve it?
Since Vuex
state storage is inherently reactivestore
, the easiest way to read state from an instance is 计算属性
to return a state in .
B 页面
Introduce the following code :
computed: {
myValue() {
return this.$store.state.someValue
}
}
At this point, when the value A 页面
is changed in some way , the value in is automatically updated.this.$store.state.someValue
B 页面
myValue
If the listener referred to by the subject is to trigger other actions after this value changes, you need to B 页面
add the listener attribute:
watch: {
myValue: function(newVal, oldVal) {
//其他业务代码
}
}