Complete the code to define an action that commits a mutation.
const actions = {
increment({ commit }) {
commit([1], 1)
}
}The action commits the mutation named incrementCount to update the state.
Complete the code to dispatch an action named 'fetchData'.
methods: {
load() {
this.$store.[1]('fetchData')
}
}Use dispatch to call an action in Vuex.
Fix the error in the action to correctly commit a mutation with a payload.
const actions = {
updateName({ commit }, name) {
commit([1], name)
}
}The mutation name must be a string, so it needs quotes around it.
Fill both blanks to create an action that commits a mutation with a payload and returns a promise.
const actions = {
saveData({ [1] }, payload) {
return new Promise((resolve) => {
[2]('saveData', payload)
resolve()
})
}
}The action uses commit to call the mutation and returns a promise to signal completion.
Fill all three blanks to define an action that commits a mutation, handles an error, and returns a promise.
const actions = {
async fetchUser({ [1] }, userId) {
try {
const user = await api.getUser(userId)
[2]('setUser', user)
return user
} catch (error) {
[3](error)
}
}
}The action destructures commit to call the mutation, and uses console.error to log errors.