Actions, mapActions & An Example in Vuex
529 بار بازدید -
3 سال پیش
-
Actions are similar to mutations,
Actions are similar to mutations, the differences being that:
Instead of mutating the state, actions commit mutations.
Actions can contain arbitrary asynchronous operations.
This may look silly at first sight: if we want to increment the count, why don't we just call store.commit('increment') directly? Remember that mutations have to be synchronous. Actions don't. We can perform asynchronous operations inside an action:
actions: {
incrementAsync ({ commit }) {
setTimeout(() return {
commit('increment')
}, 1000)
}
}
Instead of mutating the state, actions commit mutations.
Actions can contain arbitrary asynchronous operations.
This may look silly at first sight: if we want to increment the count, why don't we just call store.commit('increment') directly? Remember that mutations have to be synchronous. Actions don't. We can perform asynchronous operations inside an action:
actions: {
incrementAsync ({ commit }) {
setTimeout(() return {
commit('increment')
}, 1000)
}
}
3 سال پیش
در تاریخ 1400/11/25 منتشر شده
است.
529
بـار بازدید شده