There is an application on Vue.js, it has a store divided into modules. How to get data from one module to another? To organize the store using vuex
- For the store, are you using vuex? - Andrey Popov
- @Andrey Popov Yes - Victor Nickel
|
1 answer
In vuex modularity does not affect access to data from the store . The main thing is that all your modules are connected.
Example
index.js
Vue.use(Vuex) export default new Vuex.Store({ modules: { moduleOne, moduleTwo } }) moduleOne.js
state: { loading: 'null' }, mutations: { setError (state, payload) { state.loading = payload } }, getters: { getError(state) { return state.loadings } }, action: { changeError({commit}, payload) { commit('setLoading', payload) } } moduleTwo.js
state: { someObject: '' }, mutations: { setSomeObject(state, payload) { state.someObject = payload } }, getters: { getSomeObject(state) { return state.someObject } }, action: { fetchSomeObject({commit}, payload) { commit('setError', null) /** какие-то асинхронные операции */ then(response => { commit('setSomeobject', response.data) }) .catch(e=> {commit('SetError', e.message)} ) You can see from this simple example that the data from moduleOne.js available in moduleTwo.js
|