axios同時請求多個接口,所有接口全部請求成功后處理數據


1、問題
axios同時請求多個接口,當所有接口全部請求成功之后,接收返回的數據,進行處理

2、解決
使用axios.all([]).then(),下面是代碼
我是在main.js中全局引入的axios,如果axios不好使的,記得引入一下
main.js

import axios from "axios";
Vue.prototype.$axios = axios;
axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded';


組件中

this.$axios.all([
    this.$axios.get(url).then(res => res.data),
    this.$axios.get(url).then(res => res.data)
  ]).then(
    this.$axios.spread((val1,val2) => {
    // val 是數組中每個接口返回的值 res.data
    console.log('兩個接口全部加載完成',val1,val2) ; // 1,2
  })
).catch(err => {
  console.log(err) ;
})

 

————————————————
版權聲明:本文為CSDN博主「Janus_lian」的原創文章,遵循 CC 4.0 BY-SA 版權協議,轉載請附上原文出處鏈接及本聲明。
原文鏈接:https://blog.csdn.net/Janus_lian/article/details/101459628


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM