vue.js - vuejs 2 how to watch store values from vuex -
i using vuex
, vuejs 2
together.
i new vuex
, want watch store
variable change.
i want add watch
function in vue component
this have far:
import vue 'vue'; import { my_state, } './../../mutation-types'; export default { [my_state](state, token) { state.my_state = token; }, };
i want know if there changes in my_state
how watch store.my_state
in vuejs component?
you should not use component's watchers listen state change. recommend use getters functions , map them inside component.
import { mapgetters } 'vuex' export default { computed: { ...mapgetters({ mystate: 'getmystate' }) } }
in store:
const getters = { getmystate: state => state.my_state }
you should able listen changes made store using this.mystate
in component.
https://vuex.vuejs.org/en/getters.html#the-mapgetters-helper
Comments
Post a Comment