Blame view

src/router/helper/routeHelper.ts 2.82 KB
vben authored
1
2
3
4
5
import type { AppRouteModule, AppRouteRecordRaw } from '/@/router/types';
import type { RouteLocationNormalized, RouteRecordNormalized } from 'vue-router';

import { getParentLayout, LAYOUT } from '/@/router/constant';
import { cloneDeep } from 'lodash-es';
vben authored
6
import { warn } from '/@/utils/log';
vben authored
7
8
9
10
11
export type LayoutMapKey = 'LAYOUT';

const LayoutMap = new Map<LayoutMapKey, () => Promise<typeof import('*.vue')>>();
12
13
const dynamicViewsModules = import.meta.glob('../../views/**/*.{vue,tsx}');
vben authored
14
15
16
17
18
19
20
// 动态引入
function asyncImportRoute(routes: AppRouteRecordRaw[] | undefined) {
  if (!routes) return;
  routes.forEach((item) => {
    const { component, name } = item;
    const { children } = item;
    if (component) {
vben authored
21
      item.component = dynamicImport(dynamicViewsModules, component as string);
vben authored
22
23
24
25
26
27
28
    } else if (name) {
      item.component = getParentLayout(name);
    }
    children && asyncImportRoute(children);
  });
}
29
30
31
32
33
34
35
36
37
38
function dynamicImport(
  dynamicViewsModules: Record<
    string,
    () => Promise<{
      [key: string]: any;
    }>
  >,
  component: string
) {
  const keys = Object.keys(dynamicViewsModules);
vben authored
39
  const matchKeys = keys.filter((key) => {
40
41
    const k = key.replace('../../views', '');
    return k.startsWith(`${component}`) || k.startsWith(`/${component}`);
vben authored
42
43
44
  });
  if (matchKeys?.length === 1) {
    const matchKey = matchKeys[0];
45
    return dynamicViewsModules[matchKey];
vben authored
46
47
48
49
50
51
52
53
54
  }
  if (matchKeys?.length > 1) {
    warn(
      'Please do not create `.vue` and `.TSX` files with the same file name in the same hierarchical directory under the views folder. This will cause dynamic introduction failure'
    );
    return;
  }
}
vben authored
55
56
// Turn background objects into routing objects
export function transformObjToRoute<T = AppRouteModule>(routeList: AppRouteModule[]): T[] {
57
58
  LayoutMap.set('LAYOUT', LAYOUT);
vben authored
59
60
61
  routeList.forEach((route) => {
    if (route.component) {
      if ((route.component as string).toUpperCase() === 'LAYOUT') {
62
        route.component = LayoutMap.get(route.component as LayoutMapKey);
vben authored
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
      } else {
        route.children = [cloneDeep(route)];
        route.component = LAYOUT;
        route.name = `${route.name}Parent`;
        route.path = '';
        const meta = route.meta || {};
        meta.single = true;
        meta.affix = false;
        route.meta = meta;
      }
    }
    route.children && asyncImportRoute(route.children);
  });
  return (routeList as unknown) as T[];
}

// Return to the new routing structure, not affected by the original example
export function getRoute(route: RouteLocationNormalized): RouteLocationNormalized {
  if (!route) return route;
  const { matched, ...opt } = route;
  return {
    ...opt,
    matched: (matched
      ? matched.map((item) => ({
          meta: item.meta,
          name: item.name,
          path: item.path,
        }))
      : undefined) as RouteRecordNormalized[],
  };
}