vue.js 2 how to watch store values from vuex

后端 未结 18 2227
傲寒
傲寒 2020-11-27 10:26

I am using vuex and vuejs 2 together.

I am new to vuex, I want to watch a store variable change.

I want t

18条回答
  •  一生所求
    2020-11-27 11:08

    As mentioned above it is not good idea to watch changes directly in store

    But in some very rare cases it may be useful for someone, so i will leave this answer. For others cases, please see @gabriel-robert answer

    You can do this through state.$watch. Add this in your created (or where u need this to be executed) method in component

    this.$store.watch(
        function (state) {
            return state.my_state;
        },
        function () {
            //do something on data change
        },
        {
            deep: true //add this if u need to watch object properties change etc.
        }
    );
    

    More details: https://vuex.vuejs.org/api/#watch

提交回复
热议问题