vue中异步请求改为同步请求、vue中使请求同步、vue中方法(函数)添加async后在其他方法(函数)中怎么调用
vue中异步请求改为同步请求、vue中使请求同步、vue中方法(函数)添加async后在其他方法(函数)中怎么调用
·
将方法改为同步
// 方法上添加 async
async test(){
let that = this;
var formData = new FormData();
formData.append("id","1")
var ok = false;
// 请求接口前添加 await
await getAllDataById(formData).then(res =>{
if(res.data != null){
ok = res.data.ok
// 可以从这终止方法继续向下执行
// return;
}
}).catch(err =>{
console.log(err)
})
// 当上个请求返回数据后在执行这个请求
if(ok){
await getAllDataById(formData).then(res =>{
if(res.data != null){
// 操作
return true;
}
}).catch(err =>{
console.log(err)
})
}
return false;
}
在其他方法中调用添加async的方法
aaa() {
// 调用 test()方法 res 为方法的返回值
this.test().then(res => {
alert(res)
})
},
更多推荐
已为社区贡献12条内容
所有评论(0)