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
}))
}