axios并发请求

时间:2022-10-14 18:33:32

<template>
  <div class="home">
    
  </div>
</template>

<script>
// @ is an alias to /src
import axios from ‘axios‘;

export default {

  // 并发请求:同时多个请求,并统一处理返回值

  name: ‘axios2-3‘,
  created() {
    //axios.all()数组
    //axios.spread()将all()返回的数据进行分割统一处理,
    axios.all(
      [
        axios.get(‘/data.json‘),
        axios.get(‘/city.json‘),
      ]
    ).then(
      axios.spread((dataRes, cityRes)=>{
        console.log(dataRes, cityRes);
      })
    )

  },
}
</script>