Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vuex: Call getters from action

Is there a way for a dispatch/action to call a getter inside of it?

mutations: {     setData(state, data) {         state.data = data;     } } actions: {     sendDataToServer({ commit }, payload) {         // call getter (data) and assign to variable         // do async functions from the data returned     } }, getters: {     getAppData: state => () => {         return state.data;     } } 

So what's the best practice here? Using the mutation to change the state and then get the state and pass it to action which will then execute the async function or do I need to restructure my implementation?

call mutation -> get the data via getter -> call action

OR

do it all on the action (do mutation on the action and do the action/async method without the need of the getter)?

like image 978
The Bassman Avatar asked Aug 28 '18 01:08

The Bassman


People also ask

How do you call one action from another action in Vuex?

You can call another Vuex action by passing the name of that action as a string as the first argument of dispatch : const store = new Vuex. Store({ actions: { walk(context) { context. dispatch("goForward"); }, goForward(context) { // }, }, });

Can Vuex actions be async?

An action in Vuex is where you perform interaction with APIs and commit mutations. Such interactions are inherently asynchronous.


1 Answers

In addition to commit, actions has default injected parameters which are dispatch, getters and rootGetters. So you can simply write;

sendDataToServer({ commit, getters }, payload) to access getters.

like image 131
Tugay İlik Avatar answered Sep 28 '22 01:09

Tugay İlik