@oiij/auto-router 0.0.3 → 0.0.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -13,13 +13,11 @@ Use auto-Router 是一个 Vue Router 工具库,为 Vue 3 应用提供自动路
13
13
 
14
14
  - 🔄 自动解析和排序路由(支持数字前缀排序)
15
15
  - 📊 路由扁平化处理
16
- - 🎯 支持嵌套路由元数据继承
17
16
  - 📝 自动规范化路由名称
18
17
 
19
18
  ### Keep-Alive 管理 💾
20
19
 
21
20
  - 🚀 自动管理页面缓存
22
- - 📦 基于路由元数据的缓存配置
23
21
  - 🎨 支持动态缓存控制
24
22
 
25
23
  ### 加载状态管理 ⏳
@@ -56,13 +54,41 @@ yarn add @oiij/auto-router
56
54
 
57
55
  ## 快速开始 🌟
58
56
 
59
- ### 基础使用
57
+ ### 1. 安装插件
58
+
59
+ 在 Vue 应用中安装 `createAutoRouter` 插件,必须在 Vue Router 之后安装:
60
+
61
+ ```ts
62
+ import { createAutoRouter } from '@oiij/auto-router'
63
+ import { createApp } from 'vue'
64
+ import { createRouter, createWebHistory } from 'vue-router'
65
+
66
+ import { routes } from 'vue-router/auto-routes' // 自动生成的路由
67
+ import App from './App.vue'
68
+
69
+ const app = createApp(App)
70
+ const router = createRouter({
71
+ history: createWebHistory(),
72
+ routes
73
+ })
74
+
75
+ // 必须先安装 Vue Router
76
+ app.use(router)
77
+ // 然后安装自动路由插件
78
+ app.use(createAutoRouter(router, routes))
79
+
80
+ app.mount('#app')
81
+ ```
82
+
83
+ ### 2. 在组件中使用
84
+
85
+ 在 Vue 组件中使用 `useAutoRouter` 获取路由实例:
60
86
 
61
87
  ```vue
62
88
  <script setup>
63
89
  import { useAutoRouter } from '@oiij/auto-router'
64
90
 
65
- const { loading, routes, currentRoutePath } = useAutoRouter()
91
+ const { loading, routes, flattenRoutes } = useAutoRouter()
66
92
  </script>
67
93
 
68
94
  <template>
@@ -71,14 +97,14 @@ const { loading, routes, currentRoutePath } = useAutoRouter()
71
97
  加载中...
72
98
  </div>
73
99
  <div v-else>
74
- <p>当前路由: {{ currentRoutePath }}</p>
100
+ <h2>路由列表</h2>
75
101
  <nav>
76
102
  <router-link
77
103
  v-for="route in routes"
78
104
  :key="route.path"
79
105
  :to="route.path"
80
106
  >
81
- {{ route.meta?.title }}
107
+ {{ route.path }}
82
108
  </router-link>
83
109
  </nav>
84
110
  </div>
package/dist/index.d.mts CHANGED
@@ -1,7 +1,6 @@
1
1
  import { AutoRouterInstance } from "./setup-auto-router.mjs";
2
2
  import * as vue0 from "vue";
3
3
  import { App } from "vue";
4
- import * as vue_router0 from "vue-router";
5
4
  import { RouteRecordRaw, Router } from "vue-router";
6
5
 
7
6
  //#region src/index.d.ts
@@ -19,7 +18,12 @@ import { RouteRecordRaw, Router } from "vue-router";
19
18
  * </script>
20
19
  * ```
21
20
  */
22
- declare function useAutoRouter(): AutoRouterInstance;
21
+ declare function useAutoRouter(): {
22
+ loading: vue0.ComputedRef<boolean>;
23
+ routesRaw: readonly RouteRecordRaw[];
24
+ routes: RouteRecordRaw[];
25
+ flattenRoutes: RouteRecordRaw[];
26
+ };
23
27
  /**
24
28
  * 创建自动路由插件
25
29
  *
@@ -50,9 +54,6 @@ declare function createAutoRouter(router: Router, routes: readonly RouteRecordRa
50
54
  routesRaw: readonly RouteRecordRaw[];
51
55
  routes: RouteRecordRaw[];
52
56
  flattenRoutes: RouteRecordRaw[];
53
- keepAlivePath: vue0.ComputedRef<string[]>;
54
- currentRoute: vue0.ComputedRef<vue_router0.RouteLocationNormalizedLoaded>;
55
- currentRoutePath: vue0.ComputedRef<string>;
56
57
  install(app: App): void;
57
58
  };
58
59
  //#endregion
package/dist/plugin.mjs CHANGED
@@ -14,14 +14,11 @@
14
14
  * ```
15
15
  */
16
16
  function appendRouterMeta(route) {
17
- const sortMatch = route.path.match(/(\d+)_/);
18
- const sortNum = sortMatch ? Number(sortMatch[1]) : null;
19
- if (sortNum !== null && !Number.isNaN(sortNum)) route.addToMeta({ sort: sortNum });
20
- if (route.name) {
21
- const newName = route.name.replace(/\d+_/g, "");
22
- route.name = newName.startsWith("/") ? newName : `/${newName}`;
23
- if (route.path !== "") route.path = route.name;
24
- }
17
+ const reg = /(\d+)[_-]/g;
18
+ const sort = route.path.match(/(\d+)[_-]/)?.[1];
19
+ if (sort) route.addToMeta({ sort: Number(sort) });
20
+ route.path = route.path.replace(reg, "");
21
+ if (route.name) route.name = route.name.replace(reg, "");
25
22
  }
26
23
 
27
24
  //#endregion
@@ -1,39 +1,44 @@
1
- import { ComputedRef } from "vue";
2
- import { RouteLocationNormalizedLoaded, RouteRecordRaw, Router } from "vue-router";
1
+ import * as vue1 from "vue";
2
+ import { RouteRecordRaw, Router } from "vue-router";
3
3
 
4
4
  //#region src/setup-auto-router.d.ts
5
+ declare module 'vue-router' {
6
+ interface RouteMeta {
7
+ sort?: number;
8
+ }
9
+ }
5
10
  /**
6
- * 自动路由实例接口
11
+ * 设置自动路由
12
+ *
13
+ * 解析路由配置,提供路由工具方法和状态管理
14
+ *
15
+ * @param router - Vue Router 实例
16
+ * @param routesRaw - 原始路由配置数组
17
+ * @returns 自动路由实例,包含路由配置和工具方法
18
+ *
19
+ * @example
20
+ * ```ts
21
+ * import { setupAutoRouter } from '@oiij/auto-router'
22
+ * import { router } from './router'
23
+ * import { routes } from 'vue-router/auto-routes'
24
+ *
25
+ * const autoRouter = setupAutoRouter(router, routes)
26
+ * console.log(autoRouter.routes) // 排序后的路由
27
+ * console.log(autoRouter.flattenRoutes) // 扁平化路由
28
+ * ```
7
29
  */
8
- type AutoRouterInstance = {
9
- /**
10
- * 加载状态
11
- */
12
- loading: ComputedRef<boolean>;
13
- /**
14
- * 原始路由配置
15
- */
30
+ declare function setupAutoRouter(router: Router, routesRaw: readonly RouteRecordRaw[]): {
31
+ loading: vue1.ComputedRef<boolean>;
16
32
  routesRaw: readonly RouteRecordRaw[];
17
- /**
18
- * 解析并排序后的路由配置
19
- */
20
33
  routes: RouteRecordRaw[];
21
- /**
22
- * 扁平化的路由列表(将所有子路由提取到一级)
23
- */
24
34
  flattenRoutes: RouteRecordRaw[];
25
- /**
26
- * 需要缓存的路由路径列表
27
- */
28
- keepAlivePath: ComputedRef<string[]>;
29
- /**
30
- * 当前路由
31
- */
32
- currentRoute: ComputedRef<RouteLocationNormalizedLoaded>;
33
- /**
34
- * 当前路由路径
35
- */
36
- currentRoutePath: ComputedRef<string>;
37
35
  };
36
+ /**
37
+ * 自动路由实例类型
38
+ *
39
+ * @remarks
40
+ * 由 setupAutoRouter 函数返回的对象类型
41
+ */
42
+ type AutoRouterInstance = ReturnType<typeof setupAutoRouter>;
38
43
  //#endregion
39
44
  export { AutoRouterInstance };
@@ -2,84 +2,56 @@ import { computed, ref } from "vue";
2
2
  import { cloneDeep } from "es-toolkit";
3
3
 
4
4
  //#region src/setup-auto-router.ts
5
- /**
6
- * 解析路由配置
7
- *
8
- * 处理以下逻辑:
9
- * 1. 从子路由的 index 页面继承 group 元数据
10
- * 2. 将空路径的子路由路径设置为父路由路径
11
- * 3. 根据 sort 元数据排序路由
12
- *
13
- * @param routes - 原始路由配置
14
- * @returns 解析后的路由配置
15
- */
16
- function parseRoutes(routes) {
17
- return routes.map((route) => {
18
- const indexMeta = (route.children?.find((f) => f.path === ""))?.meta?.group;
5
+ function deepSortRoutes(routes) {
6
+ return routes.toSorted((a, b) => (a.meta?.sort ?? Infinity) - (b.meta?.sort ?? Infinity)).map((route) => {
19
7
  return {
20
8
  ...route,
21
- meta: {
22
- ...route.meta,
23
- ...indexMeta
24
- },
25
- children: route.children?.map((child) => ({
26
- ...child,
27
- path: child.path === "" ? route.path : child.path
28
- })).toSorted((a, b) => (a.meta?.sort ?? Infinity) - (b.meta?.sort ?? Infinity))
9
+ children: route.children ? deepSortRoutes(route.children) : void 0
29
10
  };
30
- }).toSorted((a, b) => (a.meta?.sort ?? Infinity) - (b.meta?.sort ?? Infinity));
11
+ });
31
12
  }
32
- /**
33
- * 扁平化路由配置
34
- *
35
- * 将嵌套的路由结构展平为一维数组
36
- *
37
- * @param routes - 路由配置
38
- * @returns 扁平化后的路由数组
39
- */
40
- function flattenRoutes(routes) {
41
- return cloneDeep(routes).flatMap((route) => route.children ?? route);
13
+ function flattenDeepRoutes(routes) {
14
+ return routes.flatMap((route) => {
15
+ const flattened = [route];
16
+ if (route.children && route.children.length > 0) flattened.push(...flattenDeepRoutes(route.children));
17
+ return flattened;
18
+ });
42
19
  }
43
20
  /**
44
21
  * 设置自动路由
45
22
  *
46
- * 解析路由配置,提供路由工具方法
23
+ * 解析路由配置,提供路由工具方法和状态管理
47
24
  *
48
25
  * @param router - Vue Router 实例
49
- * @returns 自动路由实例
26
+ * @param routesRaw - 原始路由配置数组
27
+ * @returns 自动路由实例,包含路由配置和工具方法
50
28
  *
51
29
  * @example
52
30
  * ```ts
53
31
  * import { setupAutoRouter } from '@oiij/auto-router'
54
32
  * import { router } from './router'
33
+ * import { routes } from 'vue-router/auto-routes'
55
34
  *
56
- * const autoRouter = setupAutoRouter(router)
57
- * console.log(autoRouter.routes) // 解析后的路由
35
+ * const autoRouter = setupAutoRouter(router, routes)
36
+ * console.log(autoRouter.routes) // 排序后的路由
58
37
  * console.log(autoRouter.flattenRoutes) // 扁平化路由
59
38
  * ```
60
39
  */
61
40
  function setupAutoRouter(router, routesRaw) {
62
41
  const loading = ref(false);
63
- router.beforeEach((to, from, next) => {
42
+ router.beforeEach(() => {
64
43
  loading.value = true;
65
- next();
66
44
  });
67
45
  router.afterEach(() => {
68
46
  loading.value = false;
69
47
  });
70
- const routes = parseRoutes(cloneDeep(routesRaw));
71
- const flattenRoutesCache = flattenRoutes(routes);
72
- const keepAlivePath = computed(() => flattenRoutesCache.filter((f) => f.meta?.keepAlive).map((m) => m.path));
73
- const currentRoute = computed(() => router.currentRoute.value);
74
- const currentRoutePath = computed(() => currentRoute.value.path);
48
+ const routes = deepSortRoutes(cloneDeep(routesRaw));
49
+ const flattenRoutes = flattenDeepRoutes(routes);
75
50
  return {
76
51
  loading: computed(() => loading.value),
77
52
  routesRaw,
78
53
  routes,
79
- flattenRoutes: flattenRoutesCache,
80
- keepAlivePath,
81
- currentRoute,
82
- currentRoutePath
54
+ flattenRoutes
83
55
  };
84
56
  }
85
57
 
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@oiij/auto-router",
3
3
  "type": "module",
4
- "version": "0.0.3",
4
+ "version": "0.0.5",
5
5
  "description": "Vue Router utilities and composables for Vue 3",
6
6
  "author": "oiij",
7
7
  "license": "MIT",
@@ -26,9 +26,6 @@
26
26
  "./plugin": {
27
27
  "types": "./dist/plugin.d.mts",
28
28
  "import": "./dist/plugin.mjs"
29
- },
30
- "./auto-router": {
31
- "types": "./auto-router.d.ts"
32
29
  }
33
30
  },
34
31
  "main": "./dist/index.mjs",
@@ -37,7 +34,6 @@
37
34
  "files": [
38
35
  "LICENSE",
39
36
  "README.md",
40
- "auto-router.d.ts",
41
37
  "dist",
42
38
  "package.json"
43
39
  ],
package/auto-router.d.ts DELETED
@@ -1,93 +0,0 @@
1
- /* eslint-disable */
2
- /* prettier-ignore */
3
- // biome-ignore format: off
4
- // biome-ignore lint: off
5
- // @ts-nocheck
6
- // Vue Router 路由元数据类型扩展
7
- // 这个文件扩展了 vue-router 的 RouteMeta 接口
8
- // 确保此文件包含在项目的 tsconfig.json 中
9
-
10
- import 'vue-router'
11
-
12
- // 为了确保这个文件被当作一个模块,添加至少一个 export 声明
13
- export {}
14
-
15
- declare module 'vue-router' {
16
- interface RouteMeta {
17
- /**
18
- * 路由标题
19
- */
20
- title?: string
21
-
22
- /**
23
- * 路由描述
24
- */
25
- description?: string
26
-
27
- /**
28
- * 路由图标
29
- */
30
- icon?: string
31
-
32
- /**
33
- * 图标颜色
34
- */
35
- iconColor?: string
36
-
37
- /**
38
- * 排序权重(数字越小越靠前)
39
- */
40
- sort?: number
41
-
42
- /**
43
- * 布局组件名称
44
- */
45
- layout?: string
46
-
47
- /**
48
- * 过渡动画名称
49
- */
50
- transitionName?: string
51
-
52
- /**
53
- * 是否启用 Keep-Alive 缓存
54
- */
55
- keepAlive?: boolean
56
-
57
- /**
58
- * 是否为根路由
59
- */
60
- root?: boolean
61
-
62
- /**
63
- * 路由分组信息(用于导航菜单等)
64
- */
65
- group?: {
66
- /**
67
- * 分组标题
68
- */
69
- title?: string
70
-
71
- /**
72
- * 分组描述
73
- */
74
- description?: string
75
-
76
- /**
77
- * 分组图标
78
- */
79
- icon?: string
80
-
81
- /**
82
- * 分组图标颜色
83
- */
84
- iconColor?: string
85
-
86
- /**
87
- * 分组排序权重
88
- */
89
- sort?: number
90
- }
91
- }
92
- }
93
-