在vue中循环调用接口-promise.all()

methods: {
  handleAdd (arr) {
     this.loading = true
     const allApi = []
     arr.forEach((item, index) => {
       const data = {
         id: item.id,
         name: item.name
       }
       const oneApi = api.add(data).then(res => {
         if (res.error_code === 0) {
           this.$message.success(res.msg)
         } else {
           this.$message.error(res.msg)
         }
       })
       allApi.push(oneApi)
     })
     Promise.all(allApi).then(() => {
       this.loading = false
     })
   }
}

原文链接:https://blog.csdn.net/m0_46251434/article/details/114324020  

posted @ 2022-08-22 11:20  liuyulu  阅读(1267)  评论(0)    收藏  举报