Axios 類似於for迴圈傳送批量請求{:axios.all axios.spread}。
阿新 • • 發佈:2021-08-21
Axios的請求都是非同步的!不能用for迴圈遍歷去批量傳送請求
那如果我們需要類似與這樣的請求怎麼辦呢
for(let i =0;i<array.length;i++){
axios.post(contentPath + 'invoice/new/updatetitle', JSON.stringify(sendTaitol), {
headers: {
"Content-TYpe": "application/json;charset=utf-8"
}
});
}
Axios官方也是支援的
主要是:axios.all 方法和 axios.spread方法的運用
let requestArray = new Array();//建立一個儲存需要傳送的請求 requestArray.push(this.bingfaSendRequestByPhone());//為請求陣列新增具體的請求 requestArray.push(this.bingfaSendRequestByTital()); requestArray.push(this.bingfaSendRequestByTuiSong()); axios.all(requestArray).then( axios.spread((...resp) => {//可變 ...擴充套件運算子將陣列變成一個引數序列 let flagByRequest = true;//標誌位初始化定製false let flagByRequestIndex = "";//失敗資料 [...resp].forEach((item, index) => { if (!item.data.success) { flagByRequest = false; flagByRequestIndex += index + ","; } }); //如果都是成功的就跳轉 if (flagByRequest) { console.log(flagByRequestIndex);//失敗的請求下標索引 } }) ).catch(error => { console.log(error) });
具體的請求陣列新增的內容形式如下Demo
let sendPhone = this.localUpdateDataToDo.SendPhoneData;
axios.post(contentPath + 'invoice/new/acceptModAcct', sendPhone, {
headers: {
"Content-TYpe": "application/json;charset=utf-8"
}
});
作者:隔壁老郭----------隔壁老郭還有大號:隔壁老郭---------------------------------
個性簽名:獨學而無友,則孤陋而寡聞。做一個靈魂有趣的人!
如果覺得這篇文章對你有小小的幫助的話,記得在右下角點個“推薦”哦,博主在此感謝!
萬水千山總是情,打賞一分行不行,所以如果你心情還比較高興,也是可以掃碼打賞博主,哈哈哈(っ•̀ω•́)っ✎⁾⁾!