diff --git a/package.json b/package.json index de9e5b5..a2b7da1 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "vue-admin-wonderful-next", - "version": "0.0.0", + "version": "1.0.0", "scripts": { "dev": "vite", "build": "vite build" @@ -30,7 +30,7 @@ "dotenv": "^8.2.0", "sass": "^1.32.8", "sass-loader": "^11.0.1", - "typescript": "^4.1.5", - "vite": "^2.0.1" + "typescript": "^4.2.2", + "vite": "^2.0.2" } -} +} \ No newline at end of file diff --git a/src/api/menu/testMenu.json b/src/api/menu/testMenu.json index 05fa16b..bf17dca 100644 --- a/src/api/menu/testMenu.json +++ b/src/api/menu/testMenu.json @@ -5,7 +5,7 @@ { "path": "/home", "name": "home", - "component": "'/@/views/home/index.vue'", + "component": "home/index", "meta": { "title": "首页", "isLink": "", diff --git a/src/router/index.ts b/src/router/index.ts index 69c83c7..65cccf6 100644 --- a/src/router/index.ts +++ b/src/router/index.ts @@ -476,27 +476,67 @@ const router = createRouter({ export function getBackEndControlRoutes() { getMenuTest().then((res: any) => { console.log(JSON.parse(JSON.stringify(res))); - console.log(backEndRouter(res.data)) + // console.log(backEndRouter(res.data)) dynamicRoutes[0].children = backEndRouter(res.data) + dynamicRoutes[0].children = res.data console.log(dynamicRoutes) // initAllFun() store.dispatch('setUserInfos') setAddRoute() // 添加动态路由 setFilterMenu() // 过滤权限菜单 setCacheTagsViewRoutes() // 添加 keepAlive 缓存 - console.log(router.getRoutes()) + // console.log(router.getRoutes()) }); } +// export function loadView(path: string) { +// return () => import(`../views/${path}.vue`) +// } + // 后端控制路由,递归处理每一项 `component` 中的路径 +// export function backEndRouter(routes: any) { +// if (!routes) return false +// return routes.map((v: any) => { +// if (v.component) v.component = () => import(`../views${v.component}.vue`) +// if (v.children) v.children = backEndRouter(v.children) +// return v +// }) +// } + +const dynamicViewsModules = import.meta.glob('../views/**/*.{vue,tsx}'); + +console.log(dynamicViewsModules) + + export function backEndRouter(routes: any) { - if (!routes) return false - return routes.map((v: any) => { - // if (v.component) v.component = () => import(`/@/views/${v.component}.vue`) - if (v.component) v.component = () => import(v.component) - if (v.children) v.children = backEndRouter(v.children) - return v - }) + if (!routes) return; + return routes.forEach((item: any) => { + const { component } = item; + const { children } = item; + if (component) item.component = dynamicImport(dynamicViewsModules, component as string); + children && backEndRouter(children); + return item + }); +} + +export function dynamicImport( + dynamicViewsModules: Record Promise<{ [key: string]: any; }>>, + component: string +) { + const keys = Object.keys(dynamicViewsModules); + const matchKeys = keys.filter((key) => { + const k = key.replace('../views', ''); + return k.startsWith(`${component}`) || k.startsWith(`/${component}`); + }); + console.log(matchKeys) + if (matchKeys?.length === 1) { + const matchKey = matchKeys[0]; + return dynamicViewsModules[matchKey]; + } + if (matchKeys?.length > 1) { + console.warn('Please do not create'); + return false; + } } // 多级嵌套数组处理成一维数组 @@ -602,10 +642,12 @@ export function resetRoute() { export function initAllFun() { const token = getSession('token') if (!token) return false - store.dispatch('setUserInfos') - setAddRoute() // 添加动态路由 - setFilterMenu() // 过滤权限菜单 - setCacheTagsViewRoutes() // 添加 keepAlive 缓存 + setTimeout(() => { + store.dispatch('setUserInfos') + setAddRoute() // 添加动态路由 + setFilterMenu() // 过滤权限菜单 + setCacheTagsViewRoutes() // 添加 keepAlive 缓存 + }, 1000) } // 初始化方法执行 diff --git a/src/utils/themeConfig.ts b/src/utils/themeConfig.ts index 8ece3e4..1493029 100644 --- a/src/utils/themeConfig.ts +++ b/src/utils/themeConfig.ts @@ -39,5 +39,5 @@ export default { animation: 'slideRight', columnsAsideStyle: 'columnsRound', layout: 'defaults', - isRequestRoutes: true + isRequestRoutes: false } \ No newline at end of file diff --git a/src/views/layout/index.vue b/src/views/layout/index.vue index aaf1c70..95b459c 100644 --- a/src/views/layout/index.vue +++ b/src/views/layout/index.vue @@ -6,16 +6,31 @@