@jnrs/vue-core 1.0.2 → 1.0.4

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.
@@ -1,21 +1,15 @@
1
- /**
2
- * @Author : TanRui
3
- * @WeChat : Tan578853789
4
- * @File : index.ts
5
- * @Date : 2025/11/07
6
- * @Desc. : 路由
7
- */
8
- import type { Router, RouterOptions, RouteLocationRaw, RouteLocationNormalizedGeneric } from 'vue-router';
9
- import type { MenuItem, FileModules } from './types';
1
+ import type { Router, RouteLocationRaw } from 'vue-router';
2
+ import type { CreateVueRouter, MenuItem } from './types';
10
3
  /**
11
4
  * 创建 vue-router 实例
12
5
  * @param options 配置项
13
6
  * @param fileModules 文件模块
14
7
  * @param layoutName 布局组件名称
8
+ * @param globalComponent 全局 Layout 组件(mate.global=true时需传入)
15
9
  * @param handleBeforeEach 路由前置守卫处理函数
16
10
  * @returns router 实例
17
11
  */
18
- declare const createVueRouter: (options: Partial<RouterOptions>, fileModules: FileModules, layoutName?: string, handleBeforeEach?: (to: RouteLocationNormalizedGeneric) => {}) => Router;
12
+ declare const createVueRouter: ({ options, fileModules, layoutName, globalComponent, handleBeforeEach }: CreateVueRouter) => Router;
19
13
  /**
20
14
  * 路由跳转或替换
21
15
  * @param to 路由跳转参数
@@ -1,28 +1,26 @@
1
- /**
2
- * @Author : TanRui
3
- * @WeChat : Tan578853789
4
- * @File : index.ts
5
- * @Date : 2025/11/07
6
- * @Desc. : 路由
7
- */
8
1
  import { createRouter } from 'vue-router';
9
2
  import { DEFAULT_OPTIONS } from './defaults';
10
3
  let router;
11
4
  let _fileModules;
12
5
  let _layoutName;
6
+ let _globalComponent;
13
7
  /**
14
8
  * 创建 vue-router 实例
15
9
  * @param options 配置项
16
10
  * @param fileModules 文件模块
17
11
  * @param layoutName 布局组件名称
12
+ * @param globalComponent 全局 Layout 组件(mate.global=true时需传入)
18
13
  * @param handleBeforeEach 路由前置守卫处理函数
19
14
  * @returns router 实例
20
15
  */
21
- const createVueRouter = (options, fileModules, layoutName, handleBeforeEach) => {
16
+ const createVueRouter = ({ options, fileModules, layoutName, globalComponent, handleBeforeEach }) => {
22
17
  _fileModules = fileModules;
23
18
  if (layoutName) {
24
19
  _layoutName = layoutName;
25
20
  }
21
+ if (globalComponent) {
22
+ _globalComponent = globalComponent;
23
+ }
26
24
  router = createRouter({ ...DEFAULT_OPTIONS, ...options });
27
25
  router.beforeEach(async (to) => handleBeforeEach?.(to));
28
26
  return router;
@@ -104,16 +102,31 @@ const asyncGenerateRoute = async (menus, parent) => {
104
102
  });
105
103
  continue;
106
104
  }
107
- // 构建路由记录
108
- const routeRecord = {
105
+ // 根据 global 字段决定添加为全局路由还是布局子路由
106
+ let routeRecord = {
109
107
  name: menu.name,
110
108
  path: menu.path,
111
109
  meta: menu.meta,
112
110
  redirect: menu.redirect,
113
111
  component: () => loader()
114
112
  };
115
- // 根据 global 字段决定添加为全局路由还是布局子路由
116
113
  if (menu.meta?.global) {
114
+ if (_globalComponent !== undefined) {
115
+ routeRecord = {
116
+ path: menu.path,
117
+ meta: menu.meta,
118
+ redirect: menu.redirect,
119
+ component: () => _globalComponent,
120
+ children: [
121
+ {
122
+ path: '',
123
+ name: menu.name,
124
+ meta: menu.meta,
125
+ component: () => loader()
126
+ }
127
+ ]
128
+ };
129
+ }
117
130
  router.addRoute(routeRecord);
118
131
  }
119
132
  else {
@@ -1,3 +1,10 @@
1
+ /**
2
+ * @Author : TanRui
3
+ * @WeChat : Tan578853789
4
+ * @File : @jnrs/vue-core/router
5
+ * @Date : 2025/11/05
6
+ * @Desc. : vue-router 封装
7
+ */
1
8
  export type * from 'vue-router';
2
9
  export type * from './types';
3
10
  export { useRouter, useRoute } from 'vue-router';
@@ -1,4 +1,4 @@
1
- import type { RouteRecordRaw, RouteMeta } from 'vue-router';
1
+ import type { RouteRecordRaw, RouteMeta, RouterOptions, RouteLocationNormalizedGeneric } from 'vue-router';
2
2
  export interface MenuItem {
3
3
  meta: RouteMeta;
4
4
  path: string;
@@ -12,3 +12,10 @@ export interface RouteModule {
12
12
  default: RouteRecordRaw | RouteRecordRaw[];
13
13
  }
14
14
  export type FileModules = Record<string, () => Promise<RouteModule>>;
15
+ export interface CreateVueRouter<GC = unknown> {
16
+ options: Partial<RouterOptions>;
17
+ fileModules: FileModules;
18
+ layoutName?: string;
19
+ globalComponent?: Promise<GC>;
20
+ handleBeforeEach?: (to: RouteLocationNormalizedGeneric) => void;
21
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jnrs/vue-core",
3
- "version": "1.0.2",
3
+ "version": "1.0.4",
4
4
  "description": "As the name suggests.",
5
5
  "author": "Talia-Tan",
6
6
  "license": "ISC",