In the loop traversal, call axios to achieve synchronization

The outside of forEach waits for the execution of the asynchronous request inside forEach to complete

<script>
import axios from "axios";
import { GetSecondFrom, GetSecondFromData, AddSecondFrom } from "@/api/Bpm";

export default {
  data() {
    return {
      Supplementfrom: {}, //获取表单
      all: [], //所有下拉框需要调用接口的集合----第一步
    };
  },

  methods: {
    GetSecondFrom() {
      // 二级表单渲染
      GetSecondFrom({ dataid: this.dataid }).then((res) => {
        this.Supplementfrom = res.data;
        if (res.data.dom.length > 0) {
          res.data.dom.forEach((ele, idx) => {
            if (ele.type === 7) {
                      //把所有请求的接口添加到一个数组中, 为了后期的同步-----第二步
                      this.all.push(
                        axios({
                          method: "get",
                          url: this.url,
                          headers: {
                            token: this.token,
                          },
                        }).then((re) => {
                          console.log(re.data.data, "接口");
                          v.attr = re.data.data;
                        })
                      );
            }
          });
          //所有都调用完成后,再调用其他接口-----第三步
          Promise.all(this.all).then(() => {
            // 表单数据
            GetSecondFromData({ dataid: this.dataid }).then((re) => {});
        }
      });
    },
  }

Guess you like

Origin blog.csdn.net/Qxn530/article/details/129704906