handleClick1 (data) {
return this.$http({
method: 'post',
url: 'xxxxx',
data: data
})
},
handleClick2 (data) {
return this.$http({
method: 'post',
url: 'xxxxx',
data: data
})
},
handleClick3 (data) {
return this.$http({
method: 'post',
url: 'xxxxx',
data: data
})
}
// this.$http 就是axios的別名
// 在這裏self 是this的一個別名
this.$http.all([
self.handleClick1(),
self.handleClick2(),
self.handleClick3()
]).then(this.$http.spread(function(res1, res2, res3){
console.log(res1, res2, res3);
// 這裏拿到的res就是每個方法調用後的返回值
}))
同時執行多個請求,在成功回調裏執行下一步顯然行不通,爲了防止方法之間的干擾,即使某個方法執行失敗,也不會影響其他方法的執行,這個時候axios.all 方法就很好的解決了這個問題。