async function InProcessCount() { await store.dispatch('ESourcingModule/GetMyRequest', searchView.value).then((list) => { // eslint-disable-next-line @typescript-eslint/no-unsafe-call event.InProcessCount = list.filter((o: { ViewType: string }) => o.ViewType === 'InClarification' || o.ViewType === 'SupplementEventDetails').length }) } void InProcessCount() 还是没有同步
时间: 2024-02-20 21:58:13 浏览: 28
你需要将 `async function InProcessCount()` 改为:
```
async function InProcessCount() {
const list = await store.dispatch('ESourcingModule/GetMyRequest', searchView.value)
event.InProcessCount = list.filter((o: { ViewType: string }) => o.ViewType === 'InClarification' || o.ViewType === 'SupplementEventDetails').length
}
```
这里使用了 async/await 同步获取数据,将 `await store.dispatch()` 的结果存储在变量 `list` 中,当获取到数据后再进行过滤操作,最后将结果赋值给 `event.InProcessCount`。另外,你需要删除 `void InProcessCount()` 这行代码,因为它会将函数返回值设置为 `undefined`,导致数据无法同步。
相关问题
async function InProcessCount() { const arr = await store.dispatch('ESourcingModule/GetMyRequest', searchView.value) // eslint-disable-next-line @typescript-eslint/no-unsafe-call event.InClarificationCount = arr.filter(o => o.ViewType === 'InClarification').length // eslint-disable-next-line @typescript-eslint/no-unsafe-call event.SupplementEventDetailsCount = arr.filter(o => o.ViewType === 'SupplementEventDetails').length } void InProcessCount() 优化代码
可以将函数定义改为箭头函数,并将函数名和调用合并到一起,去掉无效的 `void` 关键字,如下所示:
```
const inProcessCount = async () => {
const arr = await store.dispatch('ESourcingModule/GetMyRequest', searchView.value)
event.InClarificationCount = arr.filter(o => o.ViewType === 'InClarification').length
event.SupplementEventDetailsCount = arr.filter(o => o.ViewType === 'SupplementEventDetails').length
}
inProcessCount()
```
这样可以使代码更简洁易读。另外,函数名建议使用小驼峰命名法,以符合 JavaScript 的命名规范。
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 }优化代码
可以考虑对该函数进行如下优化:
```
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 状态。