目录:
state.js:
export default {
count: 10,
msg: 'hello vue',
lists: [
{id: 1, name: 'banana'},
{id: 2, name: 'lemon'},
{id: 3, name: 'watermelon'},
{id: 4, name: 'apple'}
]
}
getters.js:
export default {
lists(state) {
// console.log(state);
return state.lists;
},
getListById: (state) => (i) => {
return state.lists[i];
}
}
mutations.js:
export default {
addCount(state, payload) {
state.count += payload.num;
},
addCount2(state, num) {
state.count += num;
},
}
actions.js:
export default {
addCountAsyn({ commit }, payload) {
setTimeout(() => {
commit('addCount2', payload.num);
}, 1000);
}
}
store->index.js:
import Vue from 'vue'
import Vuex from 'vuex'
import state from './moduleA/state'
import getters from './moduleA/getters'
import mutations from './moduleA/mutations'
import actions from './moduleA/actions'
Vue.use(Vuex)
// Vue.config.devtools = true
export default new Vuex.Store({
state,
getters,
mutations,
actions
});