From ed968882bb6ea1e603b0c43d3d535b1e4582ed07 Mon Sep 17 00:00:00 2001 From: Soybean Date: Sat, 2 Sep 2023 00:32:13 +0800 Subject: [PATCH] fix(projects): fix route const --- .../{default-layout => base-layout}/index.vue | 0 .../src/router/elegant/imports.ts | 4 +- .../template-vue/src/router/elegant/routes.ts | 69 +++++++++++++++++-- .../src/typings/elegant-router.d.ts | 11 ++- .../views/multi-menu/first_child/index.vue | 7 ++ .../multi-menu/second_child_home/index.vue | 7 ++ packages/vue/src/context/const.ts | 23 +++++-- 7 files changed, 108 insertions(+), 13 deletions(-) rename examples/template-vue/src/layouts/{default-layout => base-layout}/index.vue (100%) create mode 100644 examples/template-vue/src/views/multi-menu/first_child/index.vue create mode 100644 examples/template-vue/src/views/multi-menu/second_child_home/index.vue diff --git a/examples/template-vue/src/layouts/default-layout/index.vue b/examples/template-vue/src/layouts/base-layout/index.vue similarity index 100% rename from examples/template-vue/src/layouts/default-layout/index.vue rename to examples/template-vue/src/layouts/base-layout/index.vue diff --git a/examples/template-vue/src/router/elegant/imports.ts b/examples/template-vue/src/router/elegant/imports.ts index 31e039c..df20015 100644 --- a/examples/template-vue/src/router/elegant/imports.ts +++ b/examples/template-vue/src/router/elegant/imports.ts @@ -8,7 +8,7 @@ import type { LastLevelRouteKey, RouteLayout } from "@elegant-router/types"; export const layouts: Record Promise)> = { - default: () => import("@/layouts/default-layout/index.vue"), + base: () => import("@/layouts/base-layout/index.vue"), }; export const views: Record Promise)> = { @@ -19,4 +19,6 @@ export const views: Record Promise import("@/views/demo-a/child2/child3/index.vue"), "demo-a_child3": () => import("@/views/demo-a/child3/index.vue"), demo3: () => import("@/views/demo3/[id].vue"), + "multi-menu_first_child": () => import("@/views/multi-menu/first_child/index.vue"), + "multi-menu_second_child_home": () => import("@/views/multi-menu/second_child_home/index.vue"), }; diff --git a/examples/template-vue/src/router/elegant/routes.ts b/examples/template-vue/src/router/elegant/routes.ts index 0fa2401..4a9f03c 100644 --- a/examples/template-vue/src/router/elegant/routes.ts +++ b/examples/template-vue/src/router/elegant/routes.ts @@ -6,7 +6,7 @@ import type { ElegantRoute } from '@elegant-router/types'; export const autoRoutes: ElegantRoute[] = [ { path: '/403', - component: 'layout.default', + component: 'layout.base', children: [ { name: '403', @@ -20,7 +20,7 @@ export const autoRoutes: ElegantRoute[] = [ }, { path: '/404', - component: 'layout.default', + component: 'layout.base', children: [ { name: '404', @@ -34,7 +34,7 @@ export const autoRoutes: ElegantRoute[] = [ }, { path: '/500', - component: 'layout.default', + component: 'layout.base', children: [ { name: '500', @@ -49,7 +49,7 @@ export const autoRoutes: ElegantRoute[] = [ { name: 'demo-a', path: '/demo-a', - component: 'layout.default', + component: 'layout.base', redirect: { name: 'demo-a_child1' }, @@ -95,7 +95,7 @@ export const autoRoutes: ElegantRoute[] = [ }, { path: '/demo3/:id', - component: 'layout.default', + component: 'layout.base', children: [ { name: 'demo3', @@ -106,5 +106,64 @@ export const autoRoutes: ElegantRoute[] = [ } } ] + }, + { + name: 'multi-menu', + path: '/multi-menu', + component: 'layout.base', + redirect: { + name: 'multi-menu_first' + }, + meta: { + title: 'multi-menu' + }, + children: [ + { + name: 'multi-menu_first', + path: '/multi-menu/first', + redirect: { + name: 'multi-menu_first_child' + }, + meta: { + title: 'multi-menu_first' + } + }, + { + name: 'multi-menu_first_child', + path: '/multi-menu/first/child', + component: 'view.multi-menu_first_child', + meta: { + title: 'multi-menu_first_child' + } + }, + { + name: 'multi-menu_second', + path: '/multi-menu/second', + redirect: { + name: 'multi-menu_second_child' + }, + meta: { + title: 'multi-menu_second' + } + }, + { + name: 'multi-menu_second_child', + path: '/multi-menu/second/child', + redirect: { + name: 'multi-menu_second_child_home' + }, + meta: { + title: 'multi-menu_second_child' + } + }, + { + name: 'multi-menu_second_child_home', + path: '/multi-menu/second/child/home', + component: 'view.multi-menu_second_child_home', + meta: { + title: 'multi-menu_second_child_home' + } + } + ] } ]; diff --git a/examples/template-vue/src/typings/elegant-router.d.ts b/examples/template-vue/src/typings/elegant-router.d.ts index 589a7ed..5dca79f 100644 --- a/examples/template-vue/src/typings/elegant-router.d.ts +++ b/examples/template-vue/src/typings/elegant-router.d.ts @@ -9,7 +9,7 @@ declare module "@elegant-router/types" { /** * route layout */ - export type RouteLayout = "default"; + export type RouteLayout = "base"; /** * route map @@ -26,6 +26,12 @@ declare module "@elegant-router/types" { "demo-a_child2_child3": "/demo-a/child2/child3"; "demo-a_child3": "/demo-a/child3"; "demo3": "/demo3/:id"; + "multi-menu": "/multi-menu"; + "multi-menu_first": "/multi-menu/first"; + "multi-menu_first_child": "/multi-menu/first/child"; + "multi-menu_second": "/multi-menu/second"; + "multi-menu_second_child": "/multi-menu/second/child"; + "multi-menu_second_child_home": "/multi-menu/second/child/home"; }; /** @@ -57,6 +63,7 @@ declare module "@elegant-router/types" { | "500" | "demo-a" | "demo3" + | "multi-menu" >; /** @@ -71,6 +78,8 @@ declare module "@elegant-router/types" { | "demo-a_child2_child3" | "demo-a_child3" | "demo3" + | "multi-menu_first_child" + | "multi-menu_second_child_home" >; /** diff --git a/examples/template-vue/src/views/multi-menu/first_child/index.vue b/examples/template-vue/src/views/multi-menu/first_child/index.vue new file mode 100644 index 0000000..9d218d1 --- /dev/null +++ b/examples/template-vue/src/views/multi-menu/first_child/index.vue @@ -0,0 +1,7 @@ + + + + + diff --git a/examples/template-vue/src/views/multi-menu/second_child_home/index.vue b/examples/template-vue/src/views/multi-menu/second_child_home/index.vue new file mode 100644 index 0000000..9d218d1 --- /dev/null +++ b/examples/template-vue/src/views/multi-menu/second_child_home/index.vue @@ -0,0 +1,7 @@ + + + + + diff --git a/packages/vue/src/context/const.ts b/packages/vue/src/context/const.ts index 012f3a1..3724102 100644 --- a/packages/vue/src/context/const.ts +++ b/packages/vue/src/context/const.ts @@ -3,7 +3,6 @@ import { existsSync } from 'node:fs'; import { writeFile } from 'node:fs/promises'; import { loadFile, generateCode } from 'magicast'; import { parse } from 'recast/parsers/typescript'; -import { PAGE_DEGREE_SPLITTER } from '@elegant-router/core'; import type { ElegantRouterTree } from '@elegant-router/core'; import { createPrefixCommentOfGenFile } from './comment'; @@ -70,19 +69,31 @@ export const autoRoutes: ElegantRoute[] = []; export function getUpdatedRouteConst(oldConst: AutoRoute[], newConst: AutoRoute[]) { const updated = newConst.map(item => { - const findItem = oldConst.find(i => (i?.name && i.name === item?.name) || i.path === item.path); + const hasName = Boolean(item?.name); + + const findItem = oldConst.find(i => (hasName && i.name === item.name) || i.path === item.path); if (!findItem) { return item; } - const isFirstLevel = !findItem?.name || (findItem.name as string).split(PAGE_DEGREE_SPLITTER).length === 1; + if (hasName) { + findItem.path = item.path; + } + + if (findItem.component) { + findItem.component = item.component; + } - if (isFirstLevel) { - return findItem; + if (findItem.redirect) { + findItem.redirect = item.redirect; } - findItem.children = getUpdatedRouteConst(findItem.children || [], item.children || []); + const children = getUpdatedRouteConst(findItem.children || [], item.children || []); + + if (children.length) { + findItem.children = children; + } return findItem; });