I have several vuex
modules. For simplicity, I'll explain my problem with an example of what I am facing:
I have 2 vuex
modules firstModule.js and secondModule.js
firstModule.js has a myArray[ ]
in its state:
//firstModule
const state = {
myArray: [//has some items]
}
In secondModule.js I have an action which fetches some data asynchronously from an external API.
This action commits a mutation which should search whether the fetched data is present in firstModule's myArray[ ]
if the fetched data is present in firstModule's
myArray[ ]
, then add it to secondModule'snewArray[ ]
const state = { newArray: [] } const mutations = { addToNewArray: (state, payload) => { function findIyem(value){ return value === payload.data; }; var item = payload.rootData.myArray.find(findItem); state.newArray.push(payload.data); } } const actions = { fetchData: ({commit, rootState}) => { // fetches some data from external API var fetched data = data // data is which I received from fetching commit('addToAnotherArray', {data: data, rootData: rootState.firstModule.myArray} } }
But, here's the problem:
according to docs mutations in modules does not get access to rootState, only actions and getters get the access to rootState
without access to rootAtate in mutations, how can I perform the logic as I performed above?
Do I have to access the rootState in the actions and pass it to the committed mutation as a payload as I have done above?