From c555bbba1db6eb63e64fcd163705ee346c1825c9 Mon Sep 17 00:00:00 2001 From: gaoxq <376340421@qq.com> Date: Tue, 2 Sep 2025 17:31:44 +0800 Subject: [PATCH] =?UTF-8?q?=E9=87=8D=E5=86=99=E5=A4=8D=E7=8E=B0=E6=96=B9?= =?UTF-8?q?=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- capi-ui/src/router/index.ts | 35 +++++++++++++++++++++++++++++------ 1 file changed, 29 insertions(+), 6 deletions(-) diff --git a/capi-ui/src/router/index.ts b/capi-ui/src/router/index.ts index bf8dc57..2850445 100644 --- a/capi-ui/src/router/index.ts +++ b/capi-ui/src/router/index.ts @@ -14,17 +14,40 @@ const constantRoutes: RouteRecordRaw[] = [ name: 'Index', component: () => import('@/views/sys/index.vue'), meta: { requiresAuth: true } - }, - { - path: '/biz/data/index', // 路由PATH,与chref对应 - name: 'DataIndex', - component: () => import('@/biz/data/index.vue') // 对应的组件 } ] +/* 2. 自动扫描 views/biz 下的所有 .vue 文件 */ +const bizModules = import.meta.glob('../views/biz/**/*.vue') + +const bizRoutes: RouteRecordRaw[] = Object.entries(bizModules).map( + ([filePath, asyncComp]) => { + // filePath 形如 ../views/biz/order/List.vue + // 去掉前缀和扩展名,得到 biz/order/List + const routePath = filePath + .replace('../views/biz/', '') + .replace('.vue', '') + .split('/') + .join('/') + + // 路由名:把路径转成 PascalCase,例如 OrderList + const routeName = routePath + .split('/') + .map((s) => s.charAt(0).toUpperCase() + s.slice(1)) + .join('') + + return { + path: `/biz/${routePath}`.replace(/\/+/g, '/'), // 防止双斜杠 + name: routeName || 'BizIndex', + component: asyncComp + } + } +) + +/* 3. 合并路由 */ const router = createRouter({ history: createWebHistory(import.meta.env.BASE_URL), - routes: [...constantRoutes] + routes: [...constantRoutes, ...bizRoutes] }) export default router \ No newline at end of file