From 7c3692c5edc29edc279c0957cd2d17628ec05972 Mon Sep 17 00:00:00 2001 From: Ryan Wang Date: Tue, 9 Aug 2022 12:05:40 +0800 Subject: [PATCH] feat: support for dynamically adding child routes to a route Signed-off-by: Ryan Wang --- packages/components/vite.config.ts | 1 + packages/shared/src/types/plugin.ts | 9 +++++++-- packages/shared/vite.config.ts | 1 + src/main.ts | 6 +++++- src/modules/contents/pages/module.ts | 5 +++-- 5 files changed, 17 insertions(+), 5 deletions(-) diff --git a/packages/components/vite.config.ts b/packages/components/vite.config.ts index 0c386ba0..5a2f7fb8 100644 --- a/packages/components/vite.config.ts +++ b/packages/components/vite.config.ts @@ -39,6 +39,7 @@ export default defineConfig({ "vue-router": "VueRouter", }, exports: "named", + generatedCode: "es5", }, }, sourcemap: true, diff --git a/packages/shared/src/types/plugin.ts b/packages/shared/src/types/plugin.ts index 5302b41b..96ca8a6d 100644 --- a/packages/shared/src/types/plugin.ts +++ b/packages/shared/src/types/plugin.ts @@ -1,5 +1,5 @@ import type { Component, Ref } from "vue"; -import type { RouteRecordRaw } from "vue-router"; +import type { RouteRecordRaw, RouteRecordName } from "vue-router"; import type { MenuGroupType } from "./menus"; import type { PagesPublicState } from "../states/pages"; @@ -7,6 +7,11 @@ export type ExtensionPointName = "PAGES" | "POSTS"; export type ExtensionPointState = PagesPublicState; +interface RouteRecordAppend { + parentName: RouteRecordName; + route: RouteRecordRaw; +} + export interface Plugin { name: string; @@ -25,7 +30,7 @@ export interface Plugin { */ deactivated?: () => void; - routes?: RouteRecordRaw[]; + routes?: RouteRecordRaw[] | RouteRecordAppend[]; menus?: MenuGroupType[]; diff --git a/packages/shared/vite.config.ts b/packages/shared/vite.config.ts index 1b7c6529..a0ae3fb0 100644 --- a/packages/shared/vite.config.ts +++ b/packages/shared/vite.config.ts @@ -38,6 +38,7 @@ export default defineConfig({ "@halo-dev/components": "HaloComponents", }, exports: "named", + generatedCode: "es5", }, }, sourcemap: true, diff --git a/src/main.ts b/src/main.ts index cf687aa1..712ccca3 100644 --- a/src/main.ts +++ b/src/main.ts @@ -47,7 +47,11 @@ function registerModule(pluginModule: Plugin) { } for (const route of pluginModule.routes) { - router.addRoute(route); + if ("parentName" in route) { + router.addRoute(route.parentName, route.route); + } else { + router.addRoute(route); + } } } diff --git a/src/modules/contents/pages/module.ts b/src/modules/contents/pages/module.ts index fdb6eedb..2f02737a 100644 --- a/src/modules/contents/pages/module.ts +++ b/src/modules/contents/pages/module.ts @@ -1,5 +1,5 @@ import { BasicLayout, definePlugin } from "@halo-dev/admin-shared"; -import SheetList from "./PageList.vue"; +import PageList from "./PageList.vue"; import { IconPages } from "@halo-dev/components"; export default definePlugin({ @@ -9,11 +9,12 @@ export default definePlugin({ { path: "/pages", component: BasicLayout, + name: "BasePages", children: [ { path: "", name: "Pages", - component: SheetList, + component: PageList, }, ], },