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