Actions are functions just like mutations, except they are not used for mutating the state but committing the mutations. Unlike mutations, actions can be asynchronous. We create action methods in the actions property in the store. An action method takes the context object as its first argument, your custom parameters as the second argument, and so forth. You can use context.commit to commit a mutation, context.state to access the state, and context.getters to access the getters. Let's add some action methods with the following steps:
- Create a store with a state property and the action methods, as follows:
// vuex-sfc/actions/basic/src/entry.js
const store = new Vuex.Store({
state: { number: 1 },
mutations: { ... },
actions: {
multiplyAsync (context) {
setTimeout(() => {
context.commit('multiply')
}, 1000)
},
multiply (context) {
context.commit('multiply')
},
multiplyBy (context, n) {
context.commit...