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