vue等两个接口都返回结果再执行下一步的实例
next只能調用一次,這邊可以用Promise.all解決,等待兩個異步操作都返回結果後再next:
beforeRouteEnter(to,from,next){
//Promise.all會等到數組內的Promise都resolve後才會繼續跑(then)
Promise.all([
main._base({
methodName:'QueryProductInfo',
productId:to.params.id
}),
main._base({
methodName:'QueryProductReview',
type:'0',
index:'0',
count:'2',
productId:to.params.id
})
])
.then(result=>next(vm=>{
//執行結果會按照上面順序放在result數組內,所以result[0],代表第一個函數的resolve結果
vm.product=result[0].data.product
vm.shop=result[0].data.shop
vm.evalData=result[1].data
}))
}
补充知识:vue中多接口请求时按顺序执行接口使用awaitasync
我就废话不多说了,大家还是直接看代码吧~
asyncgetSelectOrg(){
console.log('----1')
returnaxiosPost('/api/uum/org/orglist',{
accessToken:localStorage.token,
option:true}).then(response=>{
this.options_grade=[]
if(response.data.data.length>1){
this.options_grade.push({
value:'-1',
label:'全部'
})
this.formInline.organization='-1'
}elseif(response.data.data.length===1){
this.formInline.organization=response.data.data[0].orgCode
}
for(leti=0;i{
console.log(err)
})
},
asyncgetSelect(){
awaitthis.getSelectOrg()
console.log('----3')
this.searchInfo()
}
},
mounted(){
letthat=this
window.onresize=function(){//定义窗口大小变更通知事件
//_this.screenWidth=document.documentElement.clientWidth//窗口宽度
that.clientHeight=document.documentElement.clientHeight//窗口高度
}
this.getSelect()
},
以上这篇vue等两个接口都返回结果再执行下一步的实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持毛票票。
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:czq8825#qq.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。