简体   繁体   中英

Vuex: Cannot pass state in mutation in module pattern. Commit mutation without axios

I want to get state on my mutations .

I have store.js like :

export const store = new Vuex.Store({
  state: {
    server: 'something'
  },
  modules: {
    Product,
    Cart
  }
})

Then, on my product.js module. I can get the store server in getters but cannot make use in mutations . Ps I need to commit without axios ..

export default{

    state :{
        abc: 'asdfasf'
    },

    getters :{
        getServer : (state, getters, rootState) =>{
            return rootState.server; //It works fine.
        },

    },
    mutations :{
        UPDATE(state,rootState){
            console.log(state.getters.getServer); //this wont work
            state.abc = rootState.serve; //something like this won't work 
            either
        }
    }

}

There is no way to access rootState in mutations. But you can do it in actions and then call mutations to commit state

actions:  {
  updateServer ({commit, rootState}) {
    commit('UPDATE', rootState.serve)
  }
},
mutations: {
  UPDATE (state, payload){
     state.abc = payload
 }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM