vuex中mutation和action的区别
mutation 同步操作store, action 异步操作mutation 然后修改store
import Vue from 'vue';
import Vuex from 'vuex';
Vue.use(Vuex)
export default new Vuex.Store({
state: {
count:1
},
getters: {
newCount: state => state.count * 3
},
mutations: {
increment: (state, value) => {
console.log(value)
state.count += value;
}
},
actions: {
asyncIncrement:({commit}) => {
setTimeout(()=> {
commit('increment', 1)
}, 2000)
}
}
});
mutations方法increment只能同步变更state里的count值,actions通过调用mutations里的increment方法可以进行异步操作