Saturday, June 1, 2024
98
rated 0 times [  101] [ 3]  / answers: 1 / hits: 92545  / 6 Years ago, tue, august 28, 2018, 12:00:00

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)?


More From » ecmascript-6

 Answers
28

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.


[#53631] Friday, August 24, 2018, 6 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
jarod

Total Points: 62
Total Questions: 111
Total Answers: 83

Location: Saint Vincent and the Grenadines
Member since Sat, Sep 11, 2021
3 Years ago
jarod questions
;