refactor(route): 动态路由 component 属性支持以 / 开头或者以 .vue 和 .tsx 结尾

This commit is contained in:
zuihou 2021-09-23 12:28:21 +08:00
parent f8102446d0
commit 1abf7fdf5f

View File

@ -46,10 +46,12 @@ function dynamicImport(
) { ) {
const keys = Object.keys(dynamicViewsModules); const keys = Object.keys(dynamicViewsModules);
const matchKeys = keys.filter((key) => { const matchKeys = keys.filter((key) => {
let k = key.replace('../../views', ''); const k = key.replace('../../views', '');
const lastIndex = k.lastIndexOf('.'); const startFlag = component.startsWith('/');
k = k.substring(0, lastIndex); const endFlag = component.endsWith('.vue') || component.endsWith('.tsx');
return k === component; const startIndex = startFlag ? 0 : 1;
const lastIndex = endFlag ? k.length : k.lastIndexOf('.');
return k.substring(startIndex, lastIndex) === component;
}); });
if (matchKeys?.length === 1) { if (matchKeys?.length === 1) {
const matchKey = matchKeys[0]; const matchKey = matchKeys[0];
@ -60,7 +62,7 @@ function dynamicImport(
); );
return; return;
} else { } else {
warn('在src/views/下找不到`' + component + '.vue` 或 `' + component + '.TSX`, 请自行创建!'); warn('在src/views/下找不到`' + component + '.vue` 或 `' + component + '.tsx`, 请自行创建!');
return EXCEPTION_COMPONENT; return EXCEPTION_COMPONENT;
} }
} }
@ -82,6 +84,8 @@ export function transformObjToRoute<T = AppRouteModule>(routeList: AppRouteModul
meta.affix = false; meta.affix = false;
route.meta = meta; route.meta = meta;
} }
} else {
warn('请正确配置路由:' + route?.name + '的component属性');
} }
route.children && asyncImportRoute(route.children); route.children && asyncImportRoute(route.children);
}); });