Commit 1abf7fdf5f53ffa77a6fa0171a1d2ac21ad96118

Authored by zuihou
1 parent f8102446

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

src/router/helper/routeHelper.ts
... ... @@ -46,10 +46,12 @@ function dynamicImport(
46 46 ) {
47 47 const keys = Object.keys(dynamicViewsModules);
48 48 const matchKeys = keys.filter((key) => {
49   - let k = key.replace('../../views', '');
50   - const lastIndex = k.lastIndexOf('.');
51   - k = k.substring(0, lastIndex);
52   - return k === component;
  49 + const k = key.replace('../../views', '');
  50 + const startFlag = component.startsWith('/');
  51 + const endFlag = component.endsWith('.vue') || component.endsWith('.tsx');
  52 + const startIndex = startFlag ? 0 : 1;
  53 + const lastIndex = endFlag ? k.length : k.lastIndexOf('.');
  54 + return k.substring(startIndex, lastIndex) === component;
53 55 });
54 56 if (matchKeys?.length === 1) {
55 57 const matchKey = matchKeys[0];
... ... @@ -60,7 +62,7 @@ function dynamicImport(
60 62 );
61 63 return;
62 64 } else {
63   - warn('在src/views/下找不到`' + component + '.vue` 或 `' + component + '.TSX`, 请自行创建!');
  65 + warn('在src/views/下找不到`' + component + '.vue` 或 `' + component + '.tsx`, 请自行创建!');
64 66 return EXCEPTION_COMPONENT;
65 67 }
66 68 }
... ... @@ -82,6 +84,8 @@ export function transformObjToRoute<T = AppRouteModule>(routeList: AppRouteModul
82 84 meta.affix = false;
83 85 route.meta = meta;
84 86 }
  87 + } else {
  88 + warn('请正确配置路由:' + route?.name + '的component属性');
85 89 }
86 90 route.children && asyncImportRoute(route.children);
87 91 });
... ...