Complete Vue.js 2 Web Development_ Practical guide to building end-to-end web development solutions with Vue.js 2

(singke) #1
State Management with Vuex Chapter 21

We then have to update actions.js as this now receives both the state object and our


amount as an argument. When we use commit, let's also pass the amount through to the


mutation:


import * as types from './mutation-types';

export default {
[types.INCREMENT]({ commit }, amount) {
commit(types.INCREMENT, amount);
},
[types.DECREMENT]({ commit }, amount) {
commit(types.DECREMENT, amount);
},
[types.RESET]({ commit }) {
commit(types.RESET);
},
};

Therefore, our mutations look similar to before, but this time we increment/decrement
based on the amount:


export default {
[types.INCREMENT](state, amount) {
state.count += amount;
},
[types.DECREMENT](state, amount) {
state.count -= amount;
},
[types.RESET](state) {
state.count = 0;
},
};
Free download pdf