1. 程式人生 > >Vuex 模組化與專案例項 (2.0)

Vuex 模組化與專案例項 (2.0)

const moduleA = {
    state: {
        count: 1
    },
    mutations: {
        sayCountA(state) {
            console.log('Module A count: ', state.count);
        }
    },
    actions: {
        maAction(context) {
            context.dispatch('mbAction');
        }
    }
};

const moduleB = {
    state: {
        count: 
2 }, mutations: { sayCountB(state, num) { console.log('Module B count: ', state.count+num); } }, action: { mbAction({ commit, rootState }) { commit('sayCountA'); commit('sayCountB', rootState.a.count); } } }; const store
= { modules: { a: moduleA, b: moduleB } }; store.dispatch('maAction'); // Module A count: 1、Module B count: 3