阅读 116

Axios 类似于for循环发送批量请求{:axios.all axios.spread}。

Axios的请求都是异步的!不能用for循环遍历去批量发送请求

那如果我们需要类似与这样的请求怎么办呢

for(let i =0;i

Axios官方也是支持的

主要是:axios.all 方法和 axios.spread方法的运用

let requestArray = new Array();//建立一个存储需要发送的请求
requestArray.push(this.bingfaSendRequestByPhone());//为请求数组添加具体的请求
requestArray.push(this.bingfaSendRequestByTital());
requestArray.push(this.bingfaSendRequestByTuiSong());
axios.all(requestArray).then(
    axios.spread((...resp) => {//可变 ...扩展运算符将数组变成一个参数序列
        let flagByRequest = true;//标志位初始化定制false
        let flagByRequestIndex = "";//失败数据
        [...resp].forEach((item, index) => {
            if (!item.data.success) {
                flagByRequest = false;
                flagByRequestIndex += index + ",";
            }
        });
        //如果都是成功的就跳转
        if (flagByRequest) {
            console.log(flagByRequestIndex);//失败的请求下标索引
        }

    })
).catch(error => {
    console.log(error)
});

具体的请求数组添加的内容形式如下Demo

 let sendPhone = this.localUpdateDataToDo.SendPhoneData;
            axios.post(contentPath + ‘invoice/new/acceptModAcct‘, sendPhone, {
                headers: {
                    "Content-TYpe": "application/json;charset=utf-8"
                }
            });

原文:https://www.cnblogs.com/gtnotgod/p/15062815.html

文章分类
代码人生
版权声明:本站是系统测试站点,无实际运营。本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 XXXXXXo@163.com 举报,一经查实,本站将立刻删除。
相关推荐