Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessing getters within Vuex mutations

Within a Vuex store mutation, is it possible to access a getter? Consider the below example.

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 doesn't make much sense, because I could just access the question property directly on the state object within the mutation, but I hope you see what I am trying to do. That is, conditionally manipulating state.

Within the mutation, this is undefined and the state parameter gives access to the state object, and not the rest of the store.

The documentation on mutations doesn't mention anything about doing this.

My guess would be that it's not possible, unless I missed something? I guess the alternative would be to either perform this logic outside of the store (resulting in code duplication) or implementing an action that does this, because actions have access to the entire store context. I'm pretty sure that it's a better approach, that is to keep the mutation focused on what it's actually supposed to do; mutate the state. That's probably what I'll end up doing, but I'm just curious if accessing a getter within a mutation is even possible?

like image 542
ba0708 Avatar asked Apr 06 '17 14:04

ba0708


People also ask

How do you access getters in mutations Vuex?

To access getters within Vuex mutations with Vue. js, we just use the state in the getter and call the getter with the state . //... const store = new Vuex.

How do I access Vuex store getters?

Debug the Store In any case you can call console. log(this. $store) to debug the Store. If you do so you will see the getters are prefixed with the namespace in their name.

Can I call action from mutation Vuex?

In Vuex, actions are functions that call mutations. Actions exist because mutations must be synchronous, whereas actions can be asynchronous. You can define actions by passing a POJO as the actions property to the Vuex store constructor as shown below. To "call" an action, you should use the Store#dispatch() function.

What is the difference between Vuex mutations and actions?

The Vuex docs say: Actions are similar to mutations, the differences being that: Instead of mutating the state, actions commit mutations. Actions can contain arbitrary asynchronous operations.


1 Answers

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

This is probably bad practice*, but you could pass a reference to getters when committing a mutation like so:

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

* It is best practice to keep mutations a simple as possible, only directly affecting the Vuex state. This makes it easier to reason about state changes without having to think about any side effects. Now, if you follow best practices for vuex getters, they should not have any side effects in the first place. But, any logic that needs to reference getters can run in an action, which has read access to getters and state. Then the output of that logic can be passed to the mutation. So, you can pass the reference to the getters object to the mutation, but there's no need to and it muddies the waters.

like image 156
thanksd Avatar answered Sep 21 '22 12:09

thanksd