From f8ac82f728144b2eab0b9b080645f9cd682af462 Mon Sep 17 00:00:00 2001 From: LCJ-MinYa <1049468118@qq.com> Date: Mon, 23 Dec 2024 17:54:20 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20tools=E5=B7=A5=E5=85=B7=E5=87=BD?= =?UTF-8?q?=E6=95=B0=E5=92=8C=E8=87=AA=E5=8A=A8=E7=94=9F=E6=88=90=E8=B7=AF?= =?UTF-8?q?=E7=94=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/router/modules/demo.ts | 38 ++++++++++++++++++++++++-------------- src/utils/tools.ts | 15 +++++++++++++++ 2 files changed, 39 insertions(+), 14 deletions(-) diff --git a/src/router/modules/demo.ts b/src/router/modules/demo.ts index 675f16f..ef75461 100644 --- a/src/router/modules/demo.ts +++ b/src/router/modules/demo.ts @@ -1,4 +1,15 @@ const Layout = () => import('@/layout/index.vue'); +import { route } from '@/utils/tools'; +const titleArr = [ + { + key: 'jsx', + title: '使用jsx', + }, + { + key: 'tools', + title: '工具方法', + }, +]; // @/views/demo/**/*.vue /** @@ -22,10 +33,19 @@ const Layout = () => import('@/layout/index.vue'); const firstLevelComponents = import.meta.glob('@/views/demo/*.vue'); const secondLevelComponents = import.meta.glob('@/views/demo/*/*.vue'); const components = { ...firstLevelComponents, ...secondLevelComponents }; -console.log(components); + const demoRoutes = Object.keys(components).map((path) => { - console.log(path); - const component = components[path]; + const isHasIndex = path.includes('/index.vue'); + let cptPath = path.replace(route.FILE_NAME_PREFIX, ''); + cptPath = isHasIndex ? cptPath.replace('/index.vue', '') : path.replace('.vue', ''); + return { + path: cptPath, + name: route.convertPathToName(cptPath), + component: components[path], + meta: { + title: route.getTitleFromPathStr(titleArr, cptPath), + }, + }; }); export default { @@ -38,15 +58,5 @@ export default { title: '示例', rank: 1, }, - children: [ - { - path: '/demo/jsx', - name: 'DemoJsx', - component: () => import('@/views/demo/jsx/index.vue'), - meta: { - title: '使用jsx', - showLink: true, - }, - }, - ], + children: [...demoRoutes], } satisfies RouteConfigsTable; diff --git a/src/utils/tools.ts b/src/utils/tools.ts index 36ad1f9..476e57f 100644 --- a/src/utils/tools.ts +++ b/src/utils/tools.ts @@ -16,3 +16,18 @@ export const url = { ); }, }; + +/** route路由相关工具 */ +export const route = { + FILE_NAME_PREFIX: '/src/views/', + convertPathToName: (path: string): string => { + return path + .split('/') // 分割路径 + .map((part) => part.charAt(0).toUpperCase() + part.slice(1)) // 首字母大写 + .join(''); // 连接成字符串 + }, + getTitleFromPathStr: (arr: any, path: string): string => { + const item = arr.find(({ key }) => path.includes(key)); + return item ? item.title : null; // 如果找到,返回 title;否则返回 null + }, +};