vue 等待所有方法执行完成在执行下一步 foreach多个上传

sure

Btn() { let chooseTypeId = JSON.parse(localStorage.getItem("chooseTypeId")); if (!chooseTypeId.id || chooseTypeId.id == 0) { this.$message.error("请先选择左侧列表除全部的分类"); return; } if (this.fileList.length > 0) { const uploadPromises = this.fileList.map(item => { // 创建FormData对象 const formData = new FormData(); formData.append("file", item); formData.append("uploadName", item.name); // 发送文件上传请求,并返回Promise对象 return axios.post( "/api/blade-resource/oss/endpoint/put-file-attach-oss", formData, { headers: { "Content-Type": "multipart/form-data", }, } ); }); // 使用Promise.all等待所有上传请求完成 Promise.all(uploadPromises) .then(responses => { responses.forEach(response => { // 处理上传成功的逻辑 if (response.data.code == 200) { let data = response.data.data; // 找到目标对象的索引 const index = this.dataArray.findIndex(obj => obj.name === data.originalName); // 如果找到了目标对象,则替换为新对象 if (index !== -1) { let channelFormat = this.dataArray[index].track; let tagId = this.dataArray[index].tag; let remark = this.dataArray[index].remark; let finalData = data; this.$set(finalData, "channelFormat", channelFormat); this.$set(finalData, "tagId", tagId); this.$set(finalData, "remark", remark); // this.$set(finalData, "materialChooseType", this.typeVal); //选择类型(1图片 2视频 3音频) this.$set(finalData, "materialSettingId", chooseTypeId.id); this.$set(finalData, "materialChooseType", this.typeIndex[this.type]); submitUpload(finalData).then( () => {}, (error) => { console.error(error); } ); } } }); // 所有请求完成后执行关闭方法 this.$message({ type: "success", message: `上传成功!`, }); this.$emit("close", true); }) .catch(error => { console.error(error); }); } }