A
A
Alexey2020-11-18 12:56:19
Vue.js
Alexey, 2020-11-18 12:56:19

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

There are two components - A and B. I need that when component A made a change in the store, component B learned about it and performed some function.
I tried to fit a watch in B for this, but did not figure out how to do it. It is possible of course from B to interrogate store periodically. Maybe there is another, native way?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
Anton Anton, 2020-11-18
@alenov

watch works. For example:

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

S
StevenGer, 2020-11-18
@StevenGer

If you just want to keep track of the change, then getters. In component B, we import them into computed, useimport { mapGetters } from "vuex"

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

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

A
Alexey Yarkov, 2020-11-18
@yarkov Vue.js

I tried to fit a watch in B for this, but did not figure out how to do it

As the documentation says - do it. You could at least show that you can't do it there.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question