vue 如何依次请求 20 多个 axios 请求,同步请求多个,按次序请求

您所在的位置:网站首页 axios顺序请求 vue 如何依次请求 20 多个 axios 请求,同步请求多个,按次序请求

vue 如何依次请求 20 多个 axios 请求,同步请求多个,按次序请求

2022-10-16 20:25| 来源: 网络整理| 查看: 265

data(){ return { // 用户数据列表 userListData: [ // { id: 1, treeId, user2, label: '华强' ] } ], // 循环请求用户数据时的 标记 index loopIndex: 0, }},methods: { treeCheckAll(){ this.getAllUserInfoWithNoUpdate() }, // 只获取人员数据,不改变其它 getAllUserInfoWithNoUpdate(){ let loadingOption = { lock: true, text: "正在请求所有用户数据,请耐心等待,不要离开该页面...", background: "rgba(0, 0, 0, 0.7)" } this.loopIndex = 0 this.layerLoadingAllUser = Loading.service(loadingOption) this.getUserInfoSync() }, // 连续依次获取所有用户信息 async getUserInfoSync(){ // 每次执行之前,判断是否已经遍历所有用户,如果已经遍历所有用户,执行结束 if (this.loopIndex < this.userListData.length){ let currentUser = this.userListData[this.loopIndex]

// 获取卡片信息 let res = await doorApi.cardList({ doorId: this.doorId, employeeNo: currentUser.id }, this.cancelTokenSource.token) // 加了 await 的方法,它会依次往下执行,不会像原始 axios 那样需要回调方法一样的使用 if (res && res.length > 0){ currentUser.cards = res } else { currentUser.cards = [] }

// 获取指纹信息 let res = await doorApi.fingerprintList({ doorId: this.doorId, employeeNo: currentUser.id }, this.cancelTokenSource.token) // 加了 await 的方法,它会依次往下执行,不会像原始 axios 那样需要回调方法一样的使用 if (res && res.length > 0){ currentUser.fingerprints = res.map(item => item.fingerPrintID) } else { currentUser.fingerprints = [] }

this.loopIndex = this.loopIndex + 1 // 每 300ms 执行一下此方法 setTimeout(this.getUserInfoSync, 300) } else { // 连续请求结束后的操作 // do something } },

}



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3