vue中循環調用接口,最后生成一個數組


要用遞歸

 

//調用接口的方法  就是要循環調用他 

getOneData(){  

    return new Promise((resolve,reject)=>{
                getData(url,obj).then(res=>{
                    resolve(res)
                })
            })

}

//遞歸調用方法  index :要調用幾次既循環幾次 ,arr 是將每次調用接口的res存進去

async getCompanyCode(index,arr){
            console.log(index)
            let res = await this.getOneData()
            arr.push(res)
            if(index!==1){
                await this.getCompanyCode(index-1,arr)
            }
            return arr
        },
 
 
//在需要使用的地方  使用  3是循環3次 ,最后得到的result 就是想要的數據
let result = await this.getCompanyCode(3,[])

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM