想要获取接口返回的数据再请求另一个接口

const queryUnitList = () => {
  return new Promise((resolve,reject) => {
    getUnit()
      .then(({ data }) => {
        departList.value = data;
        resolve(data); // 加这里
      })
      .catch(error=>reject(error))
  });
};
// 编辑获取数据
const queryList = () => {
  if (name.value === "edit") {
    getUserInfoByStaffNum({ staffNum: item.value.encryptStaffNum }).then(
      ({ data }) => {
        Object.keys(ruleForm.value).map(
          (key: string) => (ruleForm.value[key] = data[key])
        );
        ruleForm.value.sex = data.sex ? Number(data.sex) : "";
        handleUnitId(departList.value); // departList.value之前获取不到数据
      }
    );
  }
};
onMounted(() => {
  Promise.all([queryRoleList(), queryUnitList()]).then(() => {
    queryList();
  });
});
在编辑的时候下拉框数据做回显 没有对应id清空
 <el-form-item label="部门:" prop="deptId">
	  <el-tree-select
	     filterable
	     check-strictly
	     style="width: 100%"
	     v-model="ruleForm.deptId"
	     :data="departList"
	     popper-class="own_select"
	     :props="defaultProps"
	   />
 </el-form-item>
 const defaultProps = {
  children: "children",
  label: "name",
  value: "id",
};

const handleUnitId = (data: any) => {
  // data为下拉框数据
  let a = "";
  let b = (data: any) => {
    data.forEach((i: any) => {
      if (i.id === ruleForm.value.deptId) {
        a = i.id;
      }
      if (i.children) {
        b(i.children);
      }
    });
  };
  b(data);
  a === "" ? (ruleForm.value.deptId = "") : (ruleForm.value.deptId = a);
  re

想要获取接口返回的数据再请求另一个接口_第1张图片

你可能感兴趣的:(前端,javascript)