Let me answer your question in general way.
First of all you need to create store with state, getters, actions and mutations.
Composition API
You need to import store inside the component via
import { useStore } from 'vuex';
And initialise it inside component like this:
export default {
setup(){
const store = useStore();
}
}
In order to watch the store changes you can use watchEffect(), do not forget to import it
import { watchEffect } from '@vue/runtime-core';
watchEffect(() => {
// pretend you have a getData getter in store
const data = store.getters.getData;
if(data === null) return;
console.log(data);
})
Moreover, you can use watch(), which is a bit older way
import { watch } from '@vue/runtime-core';
watch(
// pretend you have a getData getter in store
() => store.getters.getData,
(val, oldVal) => console.dir({ val, oldVal})
)
In some cases you will have a warnings in console like this
Invalid watch source: null A watch source can only be a getter/effect function, a ref, a reactive object, or an array of these types.
`watch(fn, options?)` signature has been moved to a separate API. Use `watchEffect(fn, options?)` instead. `watch` now only supports `watch(source, cb, options?) signature.
Option API
Classic way to watch store changes is to create a function like this
data(){
return {
todo: null
}
},
methods: {
watchStore(){
this.$store.watch(
() => this.$store.getters.getData,
data => this.todo = data
)
}
},
mounted(){
this.watchStore();
}