How to set up Vuex.store variable tracking in Vue?

  • 0
    There are two components - A and B. I need that when component A makes a change in the store, component B will know about it and perform some function.
    I tried to fit watch for this in B, but did not figure out how to do it. You can, of course, periodically poll store from B. Maybe there is another, native way?
    Vue.js Atticus Miranda, Jul 31, 2020

  • 3 Answers
  • 0
    watch works. for example:

    watch: {
    '$store.state.module.var': function () { /* ... */ }
    }
    Anonymous

  • 0
    If you just need to keep track of changes, getters. We import them into component B import {mapGetters} from "vuex"



    in computed we use

    computed: {
    ...mapGetters('nameModule', ['nameGetter'])
    }




    and then in the methods you write what you want to do if the data changes.
    Anonymous

  • 0
    I tried to install watch for this in B, but I did not figure out how to do it


    As the documentation says - so do it. You would at least show that you can't do it there.
    Ariana McCormick

Your Answer
To place the code, please use CodePen or similar tool. Thanks you!