在Vue项目中,等待所有请求完成以后执行操作

使用场景,首页渲染echarts数据,需等所有接口返回后再进行渲染

axios.all([
this.interface1(mechanismId), // 请求①
this.interface2(mechanismId) // 请求②
]).then(() => {
    this.getEchartData()  // 执行数据渲染
})

这里我的请求是封装成方法的了(注意,方法中需将请求作为返回值

interface1(mechanismId) {
  let today = Date.parse(new Date())
  return interface1IFS(mechanismId, today).then(result => {
    if (result.data.code === 200) {
      this.data1= result.data.data  // 拿到接口1的数据
}
  }).catch()
},

interface2(mechanismId) {
  let today = Date.parse(new Date())
  return interface2IFS(mechanismId,today).then(result => {
    if (result.data.code === 200) {
      this.data2= result.data.data  // 拿到接口2的数据
    }
  }).catch()
},

getEchartData () {
  Console.log(‘拿到了数据啦,可以渲染啦~’)
}
export function interface1IFS(mechanismId, startTime) {
  return axios({method: 'get', url: '/interface/interface1?mechanismId=' + mechanismId + '&startTime=' + startTime, allowCredentials: true})
}

export function interface2IFS(mechanismId, endTime) {
  let data = new FormData()
  data.append(mechanismId, mechanismId)
  data.append(endTime, endTime)
  return axios({method: 'post', url: '/interface/interface2d', data: data, allowCredentials: true})
}
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值