React 中 axios 请求All并行处理,VUE一样适用

多个请求要等全部都请求玩再做处理实现,这有时候是会用到的.这里记录下.

单独抽离demo

demo 请狠狠的戳这里  ¥  http://download.lllomh.com/cliect/#/product/J901298760373511

demo 请狠狠的戳这里  c https://download.csdn.net/download/lllomh/12792456

先上代码:

import axios from 'axios'


// 对all 的封装:
const $all = function (url, params) {
    requestNames = params.requestName
     console.log(params.requestName)
       return  axios({
            url:url,
            method:'POST',
           data:qs.stringify({
               requestName:params.requestName,
           }),
        })
}



   
 getCardPayList=()=>{
        let postData = {
            requestName:"P_CARDLIST",
        };
        return this.$axios.$all(P_CARDLIST,postData)
    }


    getAdressList=()=>{
        let postData = {
            requestName:"P_ADDRESS",
        };
        return this.$axios.$all(P_ADDRESS,postData)
    }

   

 axios.all([this.getCardPayList(),this.getAdressList()])
            .then(axios.spread((card,adress)=>{
   
                console.log(card)
                console.log(adress)
                //当这两个请求都完成的时候会触发这个函数,两个参数分别代表返回的结果
            }))


入口处: React.Component.prototype.$axios = axios;

原文地址:https://www.cnblogs.com/lllomh/p/14991883.html