问题场景:数组循环, 在每一次循环中需要调一次接口,然后把请求回来的数据,在原数组上新增一个字段children来接收

问题场景:数组循环, 在每一次循环中需要调一次接口,然后把请求回来的数据,在原数组上新增一个字段children来接收

答: 1.自执行函数
for (let i = 0; i < arr.length; i++) {
(function fn (item) {
let data = {
id: item.id,
name: item.name
}
this.$http(data).then(res => {
this.$set(item, 'children', res.tableList)
})
})(arr[i])
}

2. **promise.all方法(问题在于有一次接口报错,就获取不到数据啦)**
Promise.all(arr.map(item => {
        return this.$http({
            id: item.id,
            name: item.name
        })
    })).then(res => {
        arr.forEach((item,index) => {
            this.$set(item, 'children', res[index].tableList)
        })
    })
posted @   小白张先生  阅读(378)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· C#/.NET/.NET Core技术前沿周刊 | 第 29 期(2025年3.1-3.9)
· 从HTTP原因短语缺失研究HTTP/2和HTTP/3的设计差异
点击右上角即可分享
微信分享提示