async function bindBaseInfo () { document.title = 'RMS E-Sourcing' searchCode.value.FORM_ID = route.query.FORM_ID selected.value = [] loading.value = true event.submitData = await store.dispatch('ESourcingModule/GetMyRequestDetail', searchCode.value) originalRows = await store.dispatch('ESourcingModule/GetMyRequestFormItem', searchCode.value) rows.value = originalRows event.submitData.ProcessStep = event.submitData.STEP_ID loading.value = false logLoading.value = true logRows.value = await store.dispatch('ESourcingModule/GetApprovalLogList', searchCode.value) logLoading.value = false }优化代码
时间: 2024-02-21 22:57:04 浏览: 121
vue-form:Vue.js 2.2+的表单验证
可以考虑对该函数进行如下优化:
```
async function bindBaseInfo() {
try {
document.title = 'RMS E-Sourcing';
searchCode.value.FORM_ID = route.query.FORM_ID;
selected.value = [];
loading.value = true;
const [submitData, originalRows, logRows] = await Promise.all([
store.dispatch('ESourcingModule/GetMyRequestDetail', searchCode.value),
store.dispatch('ESourcingModule/GetMyRequestFormItem', searchCode.value),
store.dispatch('ESourcingModule/GetApprovalLogList', searchCode.value)
]);
event.submitData = submitData;
originalRows = originalRows;
event.submitData.ProcessStep = event.submitData.STEP_ID;
logLoading.value = false;
logRows.value = logRows;
} catch (error) {
console.error(error);
} finally {
loading.value = false;
}
}
```
这里使用了 `Promise.all` 来同时处理三个异步调用,以提高代码的性能。同时,还添加了 try-catch 块来捕获错误,并在最后使用 finally 块来更新 loading 状态。
阅读全文