If you're looking to utilize the Vuex helper mapMutations
with a function that operates using this
, it is indeed possible.
While there isn't an official documentation available for this feature, you can refer to the example provided in the Vuex unit test helpers.spec.js which demonstrates how it can be done.
const vm = new Vue({
store,
methods: mapMutations({
plus (commit, amount) {
commit('inc', amount + 1)
}
})
})
Furthermore, this method allows for passing parameters to mutations, which is often required in certain scenarios.
To implement this in your own code:
export default {
props: {
store: String
},
methods: {
...mapMutations({
changeModel(commit) { commit(`${this.store}/changeModel`) }
})
}
}
When used within your component, you simply call changeModel()
, as mapMutations handles injecting the commit
parameter automatically.
It's worth noting that while this approach may add complexity compared to a straightforward this.$store.commit()
method, it could be beneficial if your requirements are more intricate than those covered in the example snippet.