Sunshine_Jian 2022-01-17 16:59 采纳率: 0%
浏览 411
已结题

vue项目关于循环调接口的相关问题

问题遇到的现象和发生背景

vue项目中需要在表单里动态填多组数据,需要每一组数据去调接口,现在的情况是,比如填两组数据,会有两个这样的成功弹窗,怎么做一下优化?

问题相关代码,请勿粘贴截图
methods:{
//提交表单数据
addFormData(){
this.booksForm.booksInfo.forEach(item=>{
            //postbooks是封装好的axios请求
            postbooks({ ...item }).then((res) => {
              if (res.data.result) {
                this.$notify.success({
                  title: "成功",
                  message: "新建成功!"
                });
                };
                //重新加载数据
                this.onSubmit();
              } else {
                this.$notify.error({
                  title: "错误",
                  message: res.data.message,
                });
              }
            });
          })
        }
      });
}
}
运行结果及报错内容
我的解答思路和尝试过的方法
我想要达到的结果
  • 写回答

3条回答 默认 最新

  • 归来巨星 前端领域新星创作者 2022-01-17 17:09
    关注
    
        methods:{
          //提交表单数据
          async addFormData(){
            const isAllTrue = []
          for(let i=0;i<this.booksForm.booksInfo.length;i++){
                      //postbooks是封装好的axios请求
                     await postbooks({ ...this.booksForm.booksInfo[i] }).then((res) => {
                        if(!res.data.result){
                          isAllTrue.push(res.data)
                        }
                      });
                    })
                       if (isAllTrue.length==0) {
                          this.$notify.success({
                            title: "成功",
                            message: "新建成功!"
                          });
                          };
                          //重新加载数据
                          this.onSubmit();
                        } else {
                          let errorMsg = isAllTrue.map(item=>item.message)
                          
                          this.$notify.error({
                            title: "错误",
                            message: `项目${errorMsg}创建失败`,
                          });
                        }
                  }
                });
          }
          }
    
    评论 编辑记录

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 1月17日
  • 创建了问题 1月17日

悬赏问题

  • ¥15 上传图片时提交的存储类型
  • ¥15 Ubuntu开机显示器只显示kernel,是没操作系统(相关搜索:显卡驱动)
  • ¥15 VB.NET如何绘制倾斜的椭圆
  • ¥15 在rhel8中安装qemu-kvm时遇到“cannot initialize crypto:unable to initialize gcrypt“报错”
  • ¥15 arbotix没有/cmd_vel话题
  • ¥15 paddle库安装时报错提示需要安装common、dual等库,安装了上面的库以后还是显示报错未安装,要怎么办呀?
  • ¥20 找能定制Python脚本的
  • ¥15 odoo17的分包重新供应路线如何设置?可从销售订单中实时直接触发采购订单或相关单据
  • ¥15 用C语言怎么判断字符串的输入是否符合设定?
  • ¥15 通信专业本科生论文选这两个哪个方向好研究呀