Answer the question
In order to leave comments, you need to log in
Getting asynchronous data from one module to another?
There are 2 modules in Vuex.
Module A receives data from the server and puts it in its store. Module B must take data from module A.
The problem is that when I knock from Module B through a getter or action on module A and ask to show me the data in the store, they are not there yet, i.e. they did not come from the server.
import {getProducts} from "../api";
export default {
state: {
products: [],
},
mutations: {
setProducts(state, products) {
state.products = products;
}
},
getters: {
getProducts({products}) {
return products;
},
},
actions: {
async fetchProducts({commit}) {
commit("setProducts", await getProducts());
},
}
}
export default {
state: {
size: 10,
listProduct: [],
},
mutations: {
setListProducts(state,payload) {
console.log(payload)
}
},
getters: {
listProducts(state) {
return state.listProduct;
},
getAllItemsFromProductsStore(_, getters, rootState) {
return rootState.products.products;
}
},
actions: {
setListProducts({getters, commit}) {
let localProducts = getters.getAllItemsFromProductsStore;
}
}
}
Answer the question
In order to leave comments, you need to log in
In getter - no way. In action, just run module A's action and await it. In order not to run the action of module A several times, you can additionally do a proxy-caching action-shim, which will run the desired action once and until there is a response, return the Promise of the current one to all subsequent requests.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question