Monitor variable changes in vuex

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.someValueB 页面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) {
        //其他业务代码
    }
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325132631&siteId=291194637