let inProcessCount: number | undefined async function getInProcessCount(routeritem: RouteRecordRaw) { if (inProcessCount !== undefined) { return inProcessCount; } const len = await store.dispatch('ESourcingModule/GetMyRequest', searchView.value).then((list) => { // eslint-disable-next-line @typescript-eslint/no-unsafe-return, @typescript-eslint/no-unsafe-call return list.filter((o: { ViewType: string }) => o.ViewType === 'InClarification' || o.ViewType === 'SupplementEventDetails').length; }); inProcessCount = len if (routeritem?.meta) { // eslint-disable-next-line @typescript-eslint/restrict-template-expressions routeritem.meta.title = `${routeritem.meta?.title}(${inProcessCount})` } return len as number } function CheckAdmin(routeritem: RouteRecordRaw) { document.title = 'RMS E-Sourcing' if (routeritem?.meta?.title === 'My Request') { debugger void getInProcessCount(routeritem) }}
时间: 2024-02-14 17:04:06 浏览: 120
在 `CheckAdmin` 方法中调用 `getInProcessCount` 方法时,可以使用 `await` 等待其执行完成,并获取返回值,以确保在更新 `routeritem.meta.title` 时使用的是最新的计数值。例如:
```
async function CheckAdmin(routeritem: RouteRecordRaw) {
document.title = 'RMS E-Sourcing';
if (routeritem?.meta?.title === 'My Request') {
const len = await getInProcessCount(routeritem);
routeritem.meta.title = `${routeritem.meta?.title}(${len})`;
}
}
```
这样,当 `CheckAdmin` 方法调用 `getInProcessCount` 方法时,会等待其执行完成,并获取返回值 `len`,以后再更新 `routeritem.meta.title` 时使用的就是最新的计数值。
相关问题
let inProcessCount: number | undefined; async function getInProcessCount() { if (inProcessCount !== undefined) { return inProcessCount; } const list = await store.dispatch('ESourcingModule/GetMyRequest', searchView.value); // eslint-disable-next-line @typescript-eslint/no-unsafe-call const len = list.filter((o: { ViewType: string }) => o.ViewType === 'InClarification' || o.ViewType === 'SupplementEventDetails').length; inProcessCount = len; if (eSourcingRoute) { // 如果 eSourcingRoute 变量不为 undefined const myRequestIndex = eSourcingRoute.findIndex(route => route.meta?.title === 'My Request'); if (myRequestIndex !== -1 && eSourcingRoute[myRequestIndex].meta !== undefined) { eSourcingRoute[myRequestIndex].meta.title = 'My Request (2)' } r } eturn inProcessCount as number; }
代码中的问题在于 `r` 这个字符,它是一个语法错误,应该删除掉。
另外,代码中对 `eSourcingRoute` 变量的访问也存在问题,应该修改为 `if (eSourcingRoute !== undefined)`,这样才能避免 TypeScript 的类型检查错误。具体修改后的代码如下:
```
let inProcessCount: number | undefined;
async function getInProcessCount() {
if (inProcessCount !== undefined) {
return inProcessCount;
}
const list = await store.dispatch('ESourcingModule/GetMyRequest', searchView.value);
const len = list.filter((o: { ViewType: string }) => o.ViewType === 'InClarification' || o.ViewType === 'SupplementEventDetails').length;
inProcessCount = len;
if (eSourcingRoute !== undefined) {
const myRequestIndex = eSourcingRoute.findIndex(route => route.meta?.title === 'My Request');
if (myRequestIndex !== -1 && eSourcingRoute[myRequestIndex].meta !== undefined) {
eSourcingRoute[myRequestIndex].meta.title = 'My Request (2)';
}
}
return inProcessCount as number;
}
```
修改后,代码中的问题应该就解决了。
// 获取 InProcessCount 数据 async function getInProcessCount(routeritem: RouteRecordRaw) { const len = await store.dispatch('ESourcingModule/GetMyRequest', searchView.value).then((list) => { // eslint-disable-next-line @typescript-eslint/no-unsafe-return, @typescript-eslint/no-unsafe-call return list.filter((o: { ViewType: string }) => o.ViewType === 'InClarification' || o.ViewType === 'SupplementEventDetails').length }) if (routeritem?.meta) { // eslint-disable-next-line @typescript-eslint/restrict-template-expressions routeritem.meta.title = `${routeritem.meta?.title}(${len})`; } return len as number }
可以将 `getInProcessCount` 方法的结果缓存下来,以后再调用时直接使用缓存的结果,避免重复执行。例如:
```
let inProcessCount: number | undefined;
async function getInProcessCount(routeritem: RouteRecordRaw) {
if (inProcessCount !== undefined) {
return inProcessCount;
}
const len = await store.dispatch('ESourcingModule/GetMyRequest', searchView.value).then((list) => {
return list.filter((o: { ViewType: string }) => o.ViewType === 'InClarification' || o.ViewType === 'SupplementEventDetails').length;
});
if (routeritem?.meta) {
routeritem.meta.title = `${routeritem.meta?.title}(${len})`;
}
inProcessCount = len;
return len;
}
```
这样,第一次调用 `getInProcessCount` 方法时会执行实际的操作并将结果缓存下来,以后再调用时直接返回缓存的结果,避免重复执行。注意要将 `inProcessCount` 定义在作用域范围之外,以便在多次调用时保留状态。
阅读全文