繁体   English   中英

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

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

我想state对我的mutations

我有像store.js

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

然后,在我的product.js模块上。 我可以用getters获得store server ,但不能用于mutations 附言:我需要commitaxios ..

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
        }
    }

}

无法访问突变中的rootState 但是你可以在actions做到这一点,然后调用突变来提交状态

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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