Vuex:无法通过模块模式突变中的状态。无需突变即可提交突变

时间:2019-01-29 17:04:24

标签: vue.js vuejs2 vuex

我想在我的state上获得mutations

我有store.js,例如:

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

然后,在我的product.js模块上。我可以在store中获得server getters,但不能在mutations中使用。附言:我需要commit不带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
        }
    }

}

1 个答案:

答案 0 :(得分:1)

无法访问突变中的rootState。但是您可以在actions中进行操作,然后调用突变来提交状态

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