标签导航:

点击保存后接口调用顺序问题

在点击保存后提交表单时,程序需要同时调用新增和上传接口。然而,在第一时间调用新增接口时,它无法获得上传图片的值。为了解决此问题,添加了 setTimeout。

更优的解决方法是:

  1. 在 then 内调用新增接口:在上传接口的 then 内部调用新增接口,并把上传的图片 URL 作为参数传递给新增接口。
  2. 使用 Promise 或 async/await:使用 Promise 或 async/await 来按顺序执行接口调用。这样可以确保在调用新增接口之前完成上传的过程。

修改后的代码示例:

uploadImg(formData, 'customer').then(response => {
  this.fileInModel.content = response.data[0].url;
  this.add(response.data[0].url);
});

或者:

async add() {
  const response = await uploadImg(formData, 'customer');
  this.fileInModel.content = response.data[0].url;
  this.submit();
}