vue-axios发送并发请求

  axios发送并发请求,也就是同时发送多个请求,当多个请求响应完毕,再统一拿到全部的数据进行处理。axios提供了此API让我们做到。

axios.all([axios({
  url: 'http://httpbin.org/',
  method: 'get'
}),axios({
  url: 'http://httpbin.org/',
  method: 'get'
})]).then(results => {
  console.log(results[0])
  console.log(results[1])
})

  results是一个数组,数组元素是封装着各个请求的响应结果。还有一种写法,如下:

axios.all([axios({
  url: 'http://httpbin.org/',
  method: 'get'
}),axios({
  url: 'http://httpbin.org/',
  method: 'get'
})]).then(axios.spread((res1,res2) => {
  console.log(res1)
  console.log(res2)
}))

 

posted @ 2021-04-11 16:09  爱编程DE文兄  阅读(311)  评论(0编辑  收藏  举报