ChingFeng
Sep 9, 2018 · 3 min read

VueJs Ajax篇-Axios(五)

錯誤重新查詢

設定Axios time out 時間1秒,發生錯誤再重新查詢。

◉應用

=【Html】=
— — — — — — — — — — — —
<div id="app">
<h3>Everest pizza delivery</h3>
<button @click="order" :disabled="inProgress">Order pizza</button>
<span class="spinner" v-show="inProgress">🍕</span>
<h4>Pizza wanted</h4>
<p>{{requests}}</p>
<h4>Pizzas ordered</h4>
<span v-for="pizza in responses">
{{pizza.id}} : {{pizza.req}}
</span>
</div>
— — — — — — — — — — — —
=【VueJs】=
— — — — — — — — — — — —
new Vue({
el: '#app',
data: {
inProgress: false,
requests: new Object(null),
responses: new Object(null),
counter: 0,
impatientAxios: undefined
},
created(){
this.impatientAxios = axios.create({
timeout: 3000
});
},
methods: {
order (event, oldRequest){
let request = undefined;
if(oldRequest){
request = oldRequest;
}else{
request = { req: '🍕', id: this.counter++};
}
this.inProgress = true;
this.requests[request.id] = request;
this.impatientAxios.get('http://httpstat.us/200')
.then(response => {
this.inProgress = false;
this.responses[request.id] = this.requests[request.id];
delete this.requests[request.id];
})
.catch(e => {
this.inProgress = false;
console.error(e.message);
console.error(this.requests.s);
setTimeout(this.order(evernt, request), 1000);
})
}
}
});

Object實戰

學習歷程紀錄

ChingFeng

Written by

叢林般的世界,一步步邁向未知的未來,迴盪迷途的工程師…

Object實戰

學習歷程紀錄

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade