Access to getters in Vuex mutations

In a mutation of a Vuex repository, can you access the recipient? Consider the example below.

new Vuex.Store({ state: { question: 'Is it possible to access getters within a Vuex mutation?' }, mutations: { askQuestion(state) { // TODO: Get question from getter here let question = ''; if (question) { // ... } } }, getters: { getQuestion: (state) => { return state.question; } } }); 

Of course, the example does not make much sense, because I could just access the question property directly in the state object in the mutation, but I hope you see what I'm trying to do. That is, conditionally manipulating the state.

Inside this mutation, there is undefined , and the state parameter provides access to the state object, and not to the rest of the repository.

The mutation documentation does not mention this.

I assume this is not possible if I have not missed something? I assume that an alternative would be to either execute this logic outside the repository (which will lead to code duplication), and implement an action that does this because the actions have access to the entire repository context. I am pretty sure that this is the best approach, that is, keep the mutation focused on what it really needs to do; mutate the state. This is probably what I will eventually do, but I'm just wondering if access to the getter inside the mutation is possible?

+7
vuejs2 vuex
source share
1 answer

Vuex repository mutation methods do not provide direct access to getters.

This is probably a bad practice, but you can pass a link to getters when committing a mutation, for example:

 actions: { fooAction({commit, getters}, data) { commit('FOO_MUTATION', {data, getters}) } }, mutations: { FOO_MUTATION(state, {data, getters}) { console.log(getters); } } 
+5
source share

All Articles