In vue.js app, using vuex as state management store, I need to simply change the value of a property in vuex. For this I can follow two methods:
Dispatch an
action
method, which will further commitmutation
, which eventually will change the state.The second method is to commit the
mutation
directly from the component and the mutation method will then change the state.
Currently, I'm using first method like this:
In Component:
this.$store.dispatch('updateNotice', 'This is some notice')
In Actions:
updateNotice ({state, getters, commit}, payload) {
commit('UPDATE_NOTICE', payload)
}
In Mutations:
UPDATE_NOTICE (state, payload) {
state.notice = payload
}
As you might have noticed, I'm using the action
method simply to commit a single mutation, without any other logic or async functionality.
My question is, in this case, should I not directly commit the mutation from the component itself? What is the best practice? Since using action
method in this simple case seems verbose and serve no specific purpose.
Is there any reason that I should always commit
actions from a component?
Afterall, in vuex the ...mapMutations()
have some reason to exist.
Committing Mutations in Components
in official Vuex doc. It's expected to commit mutation directly in component. – Jacob Goh