@oiij/auto-router 0.0.1 → 0.0.3

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/dist/index.d.mts CHANGED
@@ -1,5 +1,8 @@
1
1
  import { AutoRouterInstance } from "./setup-auto-router.mjs";
2
+ import * as vue0 from "vue";
2
3
  import { App } from "vue";
4
+ import * as vue_router0 from "vue-router";
5
+ import { RouteRecordRaw, Router } from "vue-router";
3
6
 
4
7
  //#region src/index.d.ts
5
8
  /**
@@ -22,23 +25,34 @@ declare function useAutoRouter(): AutoRouterInstance;
22
25
  *
23
26
  * 必须在 Vue Router 之后安装
24
27
  *
28
+ * @param router Vue Router 实例
29
+ * @param routes 路由配置数组
25
30
  * @returns Vue 插件对象
26
31
  *
27
32
  * @example
28
33
  * ```ts
29
34
  * import { createApp } from 'vue'
30
35
  * import { createRouter } from 'vue-router'
36
+ * import { routes } from 'vue-router/auto-routes'
37
+
31
38
  * import { createAutoRouter } from '@oiij/auto-router'
32
39
  *
33
40
  * const app = createApp(App)
34
41
  * const router = createRouter({ ... })
35
42
  *
36
43
  * app.use(router)
37
- * app.use(createAutoRouter())
44
+ * app.use(createAutoRouter(router, routes))
38
45
  * app.mount('#app')
39
46
  * ```
40
47
  */
41
- declare function createAutoRouter(): {
48
+ declare function createAutoRouter(router: Router, routes: readonly RouteRecordRaw[]): {
49
+ loading: vue0.ComputedRef<boolean>;
50
+ routesRaw: readonly RouteRecordRaw[];
51
+ routes: RouteRecordRaw[];
52
+ flattenRoutes: RouteRecordRaw[];
53
+ keepAlivePath: vue0.ComputedRef<string[]>;
54
+ currentRoute: vue0.ComputedRef<vue_router0.RouteLocationNormalizedLoaded>;
55
+ currentRoutePath: vue0.ComputedRef<string>;
42
56
  install(app: App): void;
43
57
  };
44
58
  //#endregion
package/dist/index.mjs CHANGED
@@ -27,29 +27,35 @@ function useAutoRouter() {
27
27
  *
28
28
  * 必须在 Vue Router 之后安装
29
29
  *
30
+ * @param router Vue Router 实例
31
+ * @param routes 路由配置数组
30
32
  * @returns Vue 插件对象
31
33
  *
32
34
  * @example
33
35
  * ```ts
34
36
  * import { createApp } from 'vue'
35
37
  * import { createRouter } from 'vue-router'
38
+ * import { routes } from 'vue-router/auto-routes'
39
+
36
40
  * import { createAutoRouter } from '@oiij/auto-router'
37
41
  *
38
42
  * const app = createApp(App)
39
43
  * const router = createRouter({ ... })
40
44
  *
41
45
  * app.use(router)
42
- * app.use(createAutoRouter())
46
+ * app.use(createAutoRouter(router, routes))
43
47
  * app.mount('#app')
44
48
  * ```
45
49
  */
46
- function createAutoRouter() {
47
- return { install(app) {
48
- const router = app.config.globalProperties.$router;
49
- if (!router) throw new Error("Router instance is not found on this Vue app. This plugin should be installed after Vue Router.");
50
- const autoRouter = setupAutoRouter(router);
51
- app.provide(__INJECTION_KEY__, autoRouter);
52
- } };
50
+ function createAutoRouter(router, routes) {
51
+ const autoRouter = setupAutoRouter(router, routes);
52
+ return {
53
+ install(app) {
54
+ app.config.globalProperties.$autoRouter = autoRouter;
55
+ app.provide(__INJECTION_KEY__, autoRouter);
56
+ },
57
+ ...autoRouter
58
+ };
53
59
  }
54
60
 
55
61
  //#endregion
@@ -1,5 +1,5 @@
1
1
  import { ComputedRef } from "vue";
2
- import { RouteLocationNormalizedLoaded, RouteRecordRaw } from "vue-router";
2
+ import { RouteLocationNormalizedLoaded, RouteRecordRaw, Router } from "vue-router";
3
3
 
4
4
  //#region src/setup-auto-router.d.ts
5
5
  /**
@@ -1,6 +1,5 @@
1
1
  import { computed, ref } from "vue";
2
2
  import { cloneDeep } from "es-toolkit";
3
- import { routes as routes$1 } from "vue-router/auto-routes";
4
3
 
5
4
  //#region src/setup-auto-router.ts
6
5
  /**
@@ -59,7 +58,7 @@ function flattenRoutes(routes) {
59
58
  * console.log(autoRouter.flattenRoutes) // 扁平化路由
60
59
  * ```
61
60
  */
62
- function setupAutoRouter(router) {
61
+ function setupAutoRouter(router, routesRaw) {
63
62
  const loading = ref(false);
64
63
  router.beforeEach((to, from, next) => {
65
64
  loading.value = true;
@@ -68,14 +67,14 @@ function setupAutoRouter(router) {
68
67
  router.afterEach(() => {
69
68
  loading.value = false;
70
69
  });
71
- const routes = parseRoutes(cloneDeep(routes$1));
70
+ const routes = parseRoutes(cloneDeep(routesRaw));
72
71
  const flattenRoutesCache = flattenRoutes(routes);
73
72
  const keepAlivePath = computed(() => flattenRoutesCache.filter((f) => f.meta?.keepAlive).map((m) => m.path));
74
73
  const currentRoute = computed(() => router.currentRoute.value);
75
74
  const currentRoutePath = computed(() => currentRoute.value.path);
76
75
  return {
77
76
  loading: computed(() => loading.value),
78
- routesRaw: routes$1,
77
+ routesRaw,
79
78
  routes,
80
79
  flattenRoutes: flattenRoutesCache,
81
80
  keepAlivePath,
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@oiij/auto-router",
3
3
  "type": "module",
4
- "version": "0.0.1",
4
+ "version": "0.0.3",
5
5
  "description": "Vue Router utilities and composables for Vue 3",
6
6
  "author": "oiij",
7
7
  "license": "MIT",