vuex - Is it possible to mutate a state prop in a module using another’s module mutator in vuejs2? -
lets have this:
const modulea = { state: { name: 'cristian' }, namespaced: true, } const moduleb = { state: { color: 'red' }, namespaced: true, } const store = new vuex.store({ state: { user: null, }, modules: { a: modulea, b: moduleb }, mutations: { setpropvalue(state, payload) { (let [key, value] of object.entries(payload)) { state[key] = value; } }, }, })
on vue instance have firebase observer when user logs in/out , change state accordingly. want achieve this:
const payload = { module: 'root', payload: { user, }, } this.$store.commit('setpropvalue', {payload});
then module:
const payload = { module: 'a', payload: { name: 'alexander', }, }
and run same logic, different load , change props module a:
this.$store.commit('setpropvalue', {payload});
instead of committing mutation directly, consider using action
this.$store.dispatch('setpropvalue', { user });
then in vuex, action
actions: { setpropvalue({ commit }, { user }) { commit("a/setname", user.name); commit("b/setcolor", user.favoritecolor); } }
each module need own 'local' mutation in order modify modules state.
see https://vuex.vuejs.org/en/modules.html details
Comments
Post a Comment