Answer the question
In order to leave comments, you need to log in
How to intercept asynchronous request data using axios without setTimeout() in Vue2?
Good afternoon,
There is such a problem. By http request I pull out data from the server for initial initialization. But there is a problem at runtime - the data that comes in responses cannot be intercepted due to the asynchrony of such requests.
The example reflects my problem. Simplified as much as possible for clarity.
export default {
data: function () {
return {
order: null,
}
},
created() {
this.initOrder();
this.initOptions();
},
methods: {
initOrder() {
HTTP.get('/orders/id=' + this.$route.params.id).then((response) =>
{
this.order = response.data;
});
},
initOptions() {
console.log(this.order); // return null
// ...
this.order.param = 'oops'; // exception cannot read property of null
// ...
setTimeout(() => {
console.log(this.order); // return expected object
this.order.param = 'oops'; // so good by setTimeout is cumbersome
}, 1000);
}
},
}
}
Answer the question
In order to leave comments, you need to log in
Gotta do it like this
data: function () {
return {
order: {
param: 'default state'
},
}
},
initOrder() {
return HTTP.get('/orders/id=' + this.$route.params.id)
.then((response) => {
this.loaded = true;
this.order = response.data;
});
},
async initOrder()
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question