簡體   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