使用場景
比如一個彈窗需要請求表格跟樹組件的資料需要發兩個請求,這時我們需要等兩個請求完成拿到資料后再去打開彈窗,
多個請求
axios.all([axios.post("/test1"),axios.post("/test2")]).then((test1,test2)=>{
console.log('兩個介面都執行完畢啦')
})
// 或者這樣
function getUserAccount() {
return axios.get('/user/12345');
}
function getUserPermissions() {
return axios.get('/user/12345/permissions');
}
axios.all([getUserAccount(), getUserPermissions()])
.then(axios.spread(function (acct, perms) {
// 兩個請求現在都執行完成
}));
像我這里介面都封裝了的話可以這樣寫
axios.all([queryMaterialByCode({}).then((res) => {
this.tableDataProp = res.data;
}),queryMaterialCatalog().then((res) => {
this.dataTreeProp = res.data;
})]).then(()=>{
this.batchShow = true;// 打開彈窗
})
多個方法
function fun1(){
return new Promise((resolve, reject) => {
/* 你的邏輯代碼 */
resolve('隨便什么資料1')
console.log("1");
});
},
function fun2(){
return new Promise((resolve, reject) => {
/* 你的邏輯代碼 */
resolve('隨便什么資料2')
console.log("2");
});
},
function fun3(){
return new Promise((resolve, reject) => {
/* 你的邏輯代碼 */
resolve('隨便什么資料3')
console.log("3");
});
},
/* 呼叫 */
function run(){
Promise.all([
this.fun1(),
this.fun2(),
this.fun3()
]).then(res => {
/* 你的邏輯代碼 */
console.log("run");
})
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/298976.html
標籤:其他
