Answer the question
In order to leave comments, you need to log in
How to asynchronously change data in data in Vue?
When switching to the route, I get an error, since the data does not have time to load, tell me how to solve the problem?
In App.vue I load data into state:
created() {
this.$store.dispatch('GET_PRODUCTS');
},
async GET_PRODUCTS({ commit }) {
try {
const response = await axios.get('http://localhost:8080/db/db.json');
commit('initProducts', response.data);
} catch (error) {
console.log(error);
}
}
initProducts(state, products) {
state.products = products;
}
productDefaultSize: (state) => (code) => {
const products = state.products;
let product = null;
for (let key in products) {
product = products[key].find((item) => item.code === +code);
if (product) {
break;
}
}
return product.meta[0];
}
data() {
return {
counter: 1,
picked: null,
loading: true,
};
}
created() {
this.picked = this.$store.getters.productDefaultSize(this.code);
this.loading = false;
}
Answer the question
In order to leave comments, you need to log in
Try to move data receiving to mounted() using await. Show some preloader before loading data.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question