@jnrs/vue-core 1.0.6 → 1.0.7

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 ADDED
@@ -0,0 +1,89 @@
1
+ # @jnrs/vue-core
2
+
3
+ ## 目录
4
+ - [@jnrs/vue-core](#jnrsvue-core)
5
+ - [目录](#目录)
6
+ - [介绍](#介绍)
7
+ - [软件架构](#软件架构)
8
+ - [安装教程](#安装教程)
9
+ - [使用示例](#使用示例)
10
+ - [API](#api)
11
+ - [@jnrs/vue-core 模块](#jnrsvue-core-模块)
12
+ - [@jnrs/vue-core/pinia 模块](#jnrsvue-corepinia-模块)
13
+ - [@jnrs/core/router 模块](#jnrscorerouter-模块)
14
+
15
+ #### 介绍
16
+ 巨能前端 vue3 生态通用包
17
+
18
+ #### 软件架构
19
+ jnrs-monorepo-frontend
20
+
21
+ #### 安装教程
22
+ ```shell
23
+ pnpm add @jnrs/vue-core
24
+ ```
25
+
26
+ #### 使用示例
27
+ ```typescript
28
+ import type { User, DictItem } from '@jnrs/vue-core'
29
+ import { useRouter, useRoute } from '@jnrs/vue-core/router'
30
+ import { useAuthStore } from '@jnrs/vue-core/pinia'
31
+ ```
32
+
33
+ ## API
34
+
35
+ #### @jnrs/vue-core 模块
36
+ 接口
37
+ - User
38
+ - DictItem
39
+ - Dict
40
+ - MenuItem
41
+ #### @jnrs/vue-core/pinia 模块
42
+ 函数
43
+ ``` typescript
44
+ /**
45
+ * useSystemStore 系统仓库
46
+ * @returns i18n 国际化
47
+ * @returns theme 主题
48
+ * @returns menuCollapse 菜单是否折叠
49
+ * @returns documentFullscreen 文档是否全屏
50
+ * @methods toggleCollapse 折叠菜单切换
51
+ * @methods toggleFullScreen 全屏切换
52
+ */
53
+ ```
54
+ ``` typescript
55
+ /**
56
+ * useAuthStore 权限仓库
57
+ * @returns hasAuthenticated 是否已登录
58
+ * @returns token 登录凭证
59
+ * @returns userInfo 用户信息
60
+ * @returns dict 字典
61
+ * @methods asyncSetAuth 设置权限信息
62
+ * @methods clearAuth 清除权限信息
63
+ */
64
+ ```
65
+ ``` typescript
66
+ /**
67
+ * useMenuStore 菜单仓库
68
+ * @returns hasFetchedAsyncMenus 菜单是否已获取
69
+ * @returns menus 菜单项
70
+ * @methods asyncSetMenus 设置菜单信息
71
+ * @methods asyncClearMenu 清除菜单信息
72
+ */
73
+ ```
74
+
75
+ #### @jnrs/core/router 模块
76
+ 接口
77
+ - RouteMeta
78
+ - RouteLocationNormalizedGeneric
79
+ - RouteLocationNormalizedLoadedGeneric
80
+ - FileModules
81
+
82
+ 函数
83
+ - useRouter 原生 vue-router 函数
84
+ - useRoute 原生 vue-router 函数
85
+ - handleRouter 路由跳转或替换
86
+ - getRoutes 获取已注册路由
87
+ - createVueRouter 创建 vue-router 实例
88
+ - asyncGenerateRoute 设置动态路由,生成路由记录
89
+
package/dist/index.d.ts CHANGED
@@ -1,2 +1 @@
1
- import * as createVueRouter from './vueRouter/createVueRouter.js';
2
- export { createVueRouter as router };
1
+ export type * from './types/base';
package/dist/index.js CHANGED
@@ -1,5 +1,4 @@
1
- import * as createVueRouter from './vueRouter/createVueRouter.js';
2
- export { createVueRouter as router };
3
-
4
- console.log('%c✨ 欢迎使用 @jnrs/vue-core', 'background: #42B883; color: #39495C; font-weight: bold; padding: 4px 8px; border-radius: 4px; font-family: "Helvetica Neue", sans-serif;');
5
- //# sourceMappingURL=index.js.map
1
+ console.log(
2
+ "%c✨ 欢迎使用 @jnrs/vue-core",
3
+ 'background: #42B883; color: #39495C; font-weight: bold; padding: 4px 8px; border-radius: 4px; font-family: "Helvetica Neue", sans-serif;'
4
+ );
@@ -0,0 +1,38 @@
1
+ import { Dict, User } from '../../types/base';
2
+ interface Auth {
3
+ token: string;
4
+ userInfo: User;
5
+ dict: Dict;
6
+ }
7
+ /**
8
+ * useAuthStore 权限仓库
9
+ * @returns hasAuthenticated 是否已登录
10
+ * @returns token 登录凭证
11
+ * @returns userInfo 用户信息
12
+ * @returns dict 字典
13
+ * @methods asyncSetAuth 设置权限信息
14
+ * @methods clearAuth 清除权限信息
15
+ */
16
+ export declare const useAuthStore: import('pinia').StoreDefinition<"@jnrs/vue-core/pinia:auth", Pick<{
17
+ hasAuthenticated: import('vue').Ref<boolean, boolean>;
18
+ token: import('vue').Ref<string | undefined, string | undefined>;
19
+ userInfo: import('vue').Ref<any, any>;
20
+ dict: import('vue').Ref<Dict | undefined, Dict | undefined>;
21
+ asyncSetAuth: (data: Auth) => Promise<void>;
22
+ asyncClearAuth: () => Promise<void>;
23
+ }, "hasAuthenticated" | "token" | "userInfo" | "dict">, Pick<{
24
+ hasAuthenticated: import('vue').Ref<boolean, boolean>;
25
+ token: import('vue').Ref<string | undefined, string | undefined>;
26
+ userInfo: import('vue').Ref<any, any>;
27
+ dict: import('vue').Ref<Dict | undefined, Dict | undefined>;
28
+ asyncSetAuth: (data: Auth) => Promise<void>;
29
+ asyncClearAuth: () => Promise<void>;
30
+ }, never>, Pick<{
31
+ hasAuthenticated: import('vue').Ref<boolean, boolean>;
32
+ token: import('vue').Ref<string | undefined, string | undefined>;
33
+ userInfo: import('vue').Ref<any, any>;
34
+ dict: import('vue').Ref<Dict | undefined, Dict | undefined>;
35
+ asyncSetAuth: (data: Auth) => Promise<void>;
36
+ asyncClearAuth: () => Promise<void>;
37
+ }, "asyncSetAuth" | "asyncClearAuth">>;
38
+ export {};
@@ -0,0 +1,186 @@
1
+ import { MenuItem } from '../../types/base';
2
+ /**
3
+ * useMenuStore 菜单仓库
4
+ * @returns hasFetchedAsyncMenus 菜单是否已获取
5
+ * @returns menus 菜单项
6
+ * @methods asyncSetMenus 设置菜单信息
7
+ * @methods asyncClearMenu 清除菜单信息
8
+ */
9
+ export declare const useMenuStore: import('pinia').StoreDefinition<"@jnrs/vue-core/pinia:menu", Pick<{
10
+ hasFetchedAsyncMenus: import('vue').Ref<boolean, boolean>;
11
+ menus: import('vue').Ref<{
12
+ meta: {
13
+ [x: string]: unknown;
14
+ [x: number]: unknown;
15
+ [x: symbol]: unknown;
16
+ uuid?: string | undefined;
17
+ title: string;
18
+ icon?: string | undefined;
19
+ noAuth?: boolean | undefined;
20
+ global?: boolean | undefined;
21
+ fullPathTitle?: string | undefined;
22
+ };
23
+ path: string;
24
+ name?: string | undefined;
25
+ todoCount?: number | undefined;
26
+ component?: string | undefined;
27
+ redirect?: string | undefined;
28
+ children?: /*elided*/ any[] | undefined;
29
+ }[], MenuItem[] | {
30
+ meta: {
31
+ [x: string]: unknown;
32
+ [x: number]: unknown;
33
+ [x: symbol]: unknown;
34
+ uuid?: string | undefined;
35
+ title: string;
36
+ icon?: string | undefined;
37
+ noAuth?: boolean | undefined;
38
+ global?: boolean | undefined;
39
+ fullPathTitle?: string | undefined;
40
+ };
41
+ path: string;
42
+ name?: string | undefined;
43
+ todoCount?: number | undefined;
44
+ component?: string | undefined;
45
+ redirect?: string | undefined;
46
+ children?: /*elided*/ any[] | undefined;
47
+ }[]>;
48
+ asyncSetMenus: (res: MenuItem[]) => Promise<{
49
+ meta: {
50
+ [x: string]: unknown;
51
+ [x: number]: unknown;
52
+ [x: symbol]: unknown;
53
+ uuid?: string | undefined;
54
+ title: string;
55
+ icon?: string | undefined;
56
+ noAuth?: boolean | undefined;
57
+ global?: boolean | undefined;
58
+ fullPathTitle?: string | undefined;
59
+ };
60
+ path: string;
61
+ name?: string | undefined;
62
+ todoCount?: number | undefined;
63
+ component?: string | undefined;
64
+ redirect?: string | undefined;
65
+ children?: /*elided*/ any[] | undefined;
66
+ }[]>;
67
+ asyncClearMenu: () => void;
68
+ }, "menus" | "hasFetchedAsyncMenus">, Pick<{
69
+ hasFetchedAsyncMenus: import('vue').Ref<boolean, boolean>;
70
+ menus: import('vue').Ref<{
71
+ meta: {
72
+ [x: string]: unknown;
73
+ [x: number]: unknown;
74
+ [x: symbol]: unknown;
75
+ uuid?: string | undefined;
76
+ title: string;
77
+ icon?: string | undefined;
78
+ noAuth?: boolean | undefined;
79
+ global?: boolean | undefined;
80
+ fullPathTitle?: string | undefined;
81
+ };
82
+ path: string;
83
+ name?: string | undefined;
84
+ todoCount?: number | undefined;
85
+ component?: string | undefined;
86
+ redirect?: string | undefined;
87
+ children?: /*elided*/ any[] | undefined;
88
+ }[], MenuItem[] | {
89
+ meta: {
90
+ [x: string]: unknown;
91
+ [x: number]: unknown;
92
+ [x: symbol]: unknown;
93
+ uuid?: string | undefined;
94
+ title: string;
95
+ icon?: string | undefined;
96
+ noAuth?: boolean | undefined;
97
+ global?: boolean | undefined;
98
+ fullPathTitle?: string | undefined;
99
+ };
100
+ path: string;
101
+ name?: string | undefined;
102
+ todoCount?: number | undefined;
103
+ component?: string | undefined;
104
+ redirect?: string | undefined;
105
+ children?: /*elided*/ any[] | undefined;
106
+ }[]>;
107
+ asyncSetMenus: (res: MenuItem[]) => Promise<{
108
+ meta: {
109
+ [x: string]: unknown;
110
+ [x: number]: unknown;
111
+ [x: symbol]: unknown;
112
+ uuid?: string | undefined;
113
+ title: string;
114
+ icon?: string | undefined;
115
+ noAuth?: boolean | undefined;
116
+ global?: boolean | undefined;
117
+ fullPathTitle?: string | undefined;
118
+ };
119
+ path: string;
120
+ name?: string | undefined;
121
+ todoCount?: number | undefined;
122
+ component?: string | undefined;
123
+ redirect?: string | undefined;
124
+ children?: /*elided*/ any[] | undefined;
125
+ }[]>;
126
+ asyncClearMenu: () => void;
127
+ }, never>, Pick<{
128
+ hasFetchedAsyncMenus: import('vue').Ref<boolean, boolean>;
129
+ menus: import('vue').Ref<{
130
+ meta: {
131
+ [x: string]: unknown;
132
+ [x: number]: unknown;
133
+ [x: symbol]: unknown;
134
+ uuid?: string | undefined;
135
+ title: string;
136
+ icon?: string | undefined;
137
+ noAuth?: boolean | undefined;
138
+ global?: boolean | undefined;
139
+ fullPathTitle?: string | undefined;
140
+ };
141
+ path: string;
142
+ name?: string | undefined;
143
+ todoCount?: number | undefined;
144
+ component?: string | undefined;
145
+ redirect?: string | undefined;
146
+ children?: /*elided*/ any[] | undefined;
147
+ }[], MenuItem[] | {
148
+ meta: {
149
+ [x: string]: unknown;
150
+ [x: number]: unknown;
151
+ [x: symbol]: unknown;
152
+ uuid?: string | undefined;
153
+ title: string;
154
+ icon?: string | undefined;
155
+ noAuth?: boolean | undefined;
156
+ global?: boolean | undefined;
157
+ fullPathTitle?: string | undefined;
158
+ };
159
+ path: string;
160
+ name?: string | undefined;
161
+ todoCount?: number | undefined;
162
+ component?: string | undefined;
163
+ redirect?: string | undefined;
164
+ children?: /*elided*/ any[] | undefined;
165
+ }[]>;
166
+ asyncSetMenus: (res: MenuItem[]) => Promise<{
167
+ meta: {
168
+ [x: string]: unknown;
169
+ [x: number]: unknown;
170
+ [x: symbol]: unknown;
171
+ uuid?: string | undefined;
172
+ title: string;
173
+ icon?: string | undefined;
174
+ noAuth?: boolean | undefined;
175
+ global?: boolean | undefined;
176
+ fullPathTitle?: string | undefined;
177
+ };
178
+ path: string;
179
+ name?: string | undefined;
180
+ todoCount?: number | undefined;
181
+ component?: string | undefined;
182
+ redirect?: string | undefined;
183
+ children?: /*elided*/ any[] | undefined;
184
+ }[]>;
185
+ asyncClearMenu: () => void;
186
+ }, "asyncSetMenus" | "asyncClearMenu">>;
@@ -0,0 +1,31 @@
1
+ /**
2
+ * useSystemStore 系统仓库
3
+ * @returns i18n 国际化
4
+ * @returns theme 主题
5
+ * @returns menuCollapse 菜单是否折叠
6
+ * @returns documentFullscreen 文档是否全屏
7
+ * @methods toggleCollapse 折叠菜单切换
8
+ * @methods toggleFullScreen 全屏切换
9
+ */
10
+ export declare const useSystemStore: import('pinia').StoreDefinition<"@jnrs/vue-core/pinia:system", Pick<{
11
+ i18n: import('vue').Ref<string, string>;
12
+ theme: import('vue').Ref<"light" | "dark", "light" | "dark">;
13
+ menuCollapse: import('vue').Ref<boolean, boolean>;
14
+ documentFullscreen: import('vue').Ref<boolean, boolean>;
15
+ toggleCollapse: () => void;
16
+ toggleFullScreen: () => Promise<void>;
17
+ }, "i18n" | "theme" | "menuCollapse" | "documentFullscreen">, Pick<{
18
+ i18n: import('vue').Ref<string, string>;
19
+ theme: import('vue').Ref<"light" | "dark", "light" | "dark">;
20
+ menuCollapse: import('vue').Ref<boolean, boolean>;
21
+ documentFullscreen: import('vue').Ref<boolean, boolean>;
22
+ toggleCollapse: () => void;
23
+ toggleFullScreen: () => Promise<void>;
24
+ }, never>, Pick<{
25
+ i18n: import('vue').Ref<string, string>;
26
+ theme: import('vue').Ref<"light" | "dark", "light" | "dark">;
27
+ menuCollapse: import('vue').Ref<boolean, boolean>;
28
+ documentFullscreen: import('vue').Ref<boolean, boolean>;
29
+ toggleCollapse: () => void;
30
+ toggleFullScreen: () => Promise<void>;
31
+ }, "toggleCollapse" | "toggleFullScreen">>;
@@ -0,0 +1,3 @@
1
+ export * from './base/system';
2
+ export * from './base/auth';
3
+ export * from './base/menu';
@@ -0,0 +1,165 @@
1
+ import "pinia-plugin-persistedstate";
2
+ import { ref as r } from "vue";
3
+ import { defineStore as a } from "pinia";
4
+ import { asyncGenerateRoute as p } from "../vueRouter/index.js";
5
+ class h {
6
+ // 默认全屏元素,可配置
7
+ defaultElement;
8
+ constructor(e) {
9
+ this.defaultElement = e || document.documentElement;
10
+ }
11
+ /**
12
+ * 进入全屏
13
+ * @param element 可选,指定要全屏的元素
14
+ */
15
+ async request(e) {
16
+ const u = e || this.defaultElement;
17
+ try {
18
+ await u.requestFullscreen({
19
+ navigationUI: "hide"
20
+ });
21
+ } catch (t) {
22
+ console.error("进入全屏失败:", t);
23
+ }
24
+ }
25
+ /**
26
+ * 退出全屏
27
+ */
28
+ async exit() {
29
+ if (document.fullscreenElement)
30
+ try {
31
+ await document.exitFullscreen();
32
+ } catch (e) {
33
+ console.error("退出全屏失败:", e);
34
+ }
35
+ }
36
+ /**
37
+ * 切换全屏状态
38
+ * @param element 可选,指定要切换全屏的元素
39
+ */
40
+ async toggle(e) {
41
+ document.fullscreenElement ? await this.exit() : await this.request(e);
42
+ }
43
+ /**
44
+ * 判断当前是否处于全屏状态
45
+ * @returns boolean
46
+ */
47
+ isFullscreen() {
48
+ return !!document.fullscreenElement;
49
+ }
50
+ /**
51
+ * 设置默认全屏元素
52
+ * @param element 新的默认元素
53
+ */
54
+ setDefaultElement(e) {
55
+ this.defaultElement = e;
56
+ }
57
+ /**
58
+ * 获取当前全屏的元素(如果存在)
59
+ * @returns HTMLElement | null
60
+ */
61
+ getFullscreenElement() {
62
+ return document.fullscreenElement;
63
+ }
64
+ }
65
+ console.log("%c✨ 欢迎使用 @jnrs/shared", 'background: #8061BF; color: #f2f2f2; font-weight: bold; padding: 4px 8px; border-radius: 4px; font-family: "Helvetica Neue", sans-serif;');
66
+ const l = new h(), F = a(
67
+ "@jnrs/vue-core/pinia:system",
68
+ () => {
69
+ const n = r("zh-CN"), e = r("light"), u = r(!1), t = r(l.isFullscreen());
70
+ return { i18n: n, theme: e, menuCollapse: u, documentFullscreen: t, toggleCollapse: () => {
71
+ u.value = !u.value;
72
+ }, toggleFullScreen: async () => {
73
+ await l.toggle(), t.value = l.isFullscreen();
74
+ } };
75
+ },
76
+ {
77
+ persist: {
78
+ pick: ["i18n", "theme", "menuCollapse"]
79
+ }
80
+ }
81
+ ), C = a(
82
+ "@jnrs/vue-core/pinia:auth",
83
+ () => {
84
+ const n = r(!1), e = r(), u = r(), t = r();
85
+ return {
86
+ hasAuthenticated: n,
87
+ token: e,
88
+ userInfo: u,
89
+ dict: t,
90
+ asyncSetAuth: async (s) => {
91
+ n.value = !0, s.token && (e.value = s.token), s.userInfo && (u.value = s.userInfo), s.dict && (t.value = s.dict);
92
+ },
93
+ asyncClearAuth: async () => {
94
+ n.value = !1, e.value = void 0, u.value = void 0, t.value = void 0;
95
+ }
96
+ };
97
+ },
98
+ {
99
+ persist: {
100
+ pick: ["hasAuthenticated", "token", "userInfo", "dict"]
101
+ }
102
+ }
103
+ ), o = [];
104
+ for (let n = 0; n < 256; ++n)
105
+ o.push((n + 256).toString(16).slice(1));
106
+ function y(n, e = 0) {
107
+ return (o[n[e + 0]] + o[n[e + 1]] + o[n[e + 2]] + o[n[e + 3]] + "-" + o[n[e + 4]] + o[n[e + 5]] + "-" + o[n[e + 6]] + o[n[e + 7]] + "-" + o[n[e + 8]] + o[n[e + 9]] + "-" + o[n[e + 10]] + o[n[e + 11]] + o[n[e + 12]] + o[n[e + 13]] + o[n[e + 14]] + o[n[e + 15]]).toLowerCase();
108
+ }
109
+ let i;
110
+ const f = new Uint8Array(16);
111
+ function g() {
112
+ if (!i) {
113
+ if (typeof crypto > "u" || !crypto.getRandomValues)
114
+ throw new Error("crypto.getRandomValues() not supported. See https://github.com/uuidjs/uuid#getrandomvalues-not-supported");
115
+ i = crypto.getRandomValues.bind(crypto);
116
+ }
117
+ return i(f);
118
+ }
119
+ const v = typeof crypto < "u" && crypto.randomUUID && crypto.randomUUID.bind(crypto), m = { randomUUID: v };
120
+ function w(n, e, u) {
121
+ n = n || {};
122
+ const t = n.random ?? n.rng?.() ?? g();
123
+ if (t.length < 16)
124
+ throw new Error("Random bytes length must be >= 16");
125
+ return t[6] = t[6] & 15 | 64, t[8] = t[8] & 63 | 128, y(t);
126
+ }
127
+ function x(n, e, u) {
128
+ return m.randomUUID && !n ? m.randomUUID() : w(n);
129
+ }
130
+ const I = a(
131
+ "@jnrs/vue-core/pinia:menu",
132
+ () => {
133
+ const n = r(!1), e = r([]), u = async (c) => {
134
+ if (n.value)
135
+ return e.value;
136
+ t(c), e.value = c, n.value = !0;
137
+ try {
138
+ await p(e.value);
139
+ } catch (s) {
140
+ throw s;
141
+ }
142
+ return e.value;
143
+ }, t = (c) => {
144
+ for (const s of c)
145
+ s.meta.uuid = x(), s.children && t(s.children);
146
+ };
147
+ return { hasFetchedAsyncMenus: n, menus: e, asyncSetMenus: u, asyncClearMenu: () => {
148
+ n.value = !1, e.value = [];
149
+ } };
150
+ },
151
+ {
152
+ persist: {
153
+ pick: ["menus"]
154
+ }
155
+ }
156
+ );
157
+ console.log(
158
+ "%c✨ 欢迎使用 @jnrs/vue-core/pinia",
159
+ 'background: #42B883; color: #39495C; font-weight: bold; padding: 4px 8px; border-radius: 4px; font-family: "Helvetica Neue", sans-serif;'
160
+ );
161
+ export {
162
+ C as useAuthStore,
163
+ I as useMenuStore,
164
+ F as useSystemStore
165
+ };
@@ -0,0 +1,44 @@
1
+ import { RouteMeta } from 'vue-router';
2
+ /**
3
+ * 用户信息
4
+ */
5
+ export interface User {
6
+ id: number;
7
+ name: string;
8
+ account?: string;
9
+ role?: number;
10
+ jobTitle?: number;
11
+ workNo?: string;
12
+ workgroup?: null | string;
13
+ avatarFileName?: string;
14
+ loginDateTime?: string;
15
+ [key: string]: unknown;
16
+ }
17
+ /**
18
+ * 字典
19
+ */
20
+ export interface DictItem {
21
+ label: string;
22
+ value: string | number;
23
+ displayColor?: string;
24
+ sequence?: number;
25
+ [key: string]: unknown;
26
+ }
27
+ /**
28
+ * 字典键值对
29
+ */
30
+ export interface Dict {
31
+ [key: string]: DictItem[];
32
+ }
33
+ /**
34
+ * 定义菜单项接口
35
+ */
36
+ export interface MenuItem {
37
+ meta: RouteMeta;
38
+ path: string;
39
+ name?: string;
40
+ todoCount?: number;
41
+ component?: string;
42
+ redirect?: string;
43
+ children?: MenuItem[];
44
+ }
@@ -1,7 +1,6 @@
1
- import * as vue_router from 'vue-router';
2
1
  import { Router, RouteLocationRaw } from 'vue-router';
3
- import { CreateVueRouter, MenuItem } from './types.js';
4
-
2
+ import { MenuItem } from '../types/base';
3
+ import { CreateVueRouter } from './types';
5
4
  /**
6
5
  * 创建 vue-router 实例
7
6
  * @param options 配置项
@@ -18,7 +17,7 @@ declare const createVueRouter: ({ options, fileModules, layoutName, globalCompon
18
17
  * @param type 默认为push, push | replace
19
18
  * @returns Promise<boolean | void>
20
19
  */
21
- declare const handleRouter: (to: RouteLocationRaw, type?: "push" | "replace") => Promise<void | vue_router.NavigationFailure>;
20
+ declare const handleRouter: (to: RouteLocationRaw, type?: "push" | "replace") => Promise<void | import('vue-router').NavigationFailure>;
22
21
  /**
23
22
  * 设置动态路由,生成路由记录
24
23
  * @param menus 菜单项
@@ -26,5 +25,4 @@ declare const handleRouter: (to: RouteLocationRaw, type?: "push" | "replace") =>
26
25
  */
27
26
  declare const asyncGenerateRoute: (menus: MenuItem[], parent?: MenuItem) => Promise<void>;
28
27
  declare const getRoutes: () => MenuItem[];
29
-
30
- export { asyncGenerateRoute, createVueRouter, getRoutes, handleRouter };
28
+ export { createVueRouter, asyncGenerateRoute, handleRouter, getRoutes };
@@ -4,7 +4,7 @@
4
4
  * @property routes - 路由记录数组
5
5
  */
6
6
  declare const DEFAULT_OPTIONS: {
7
- history: import("vue-router").RouterHistory;
7
+ history: import('vue-router').RouterHistory;
8
8
  routes: never[];
9
9
  };
10
10
  export { DEFAULT_OPTIONS };
@@ -1,4 +1,11 @@
1
- export * from 'vue-router';
2
- export { useRoute, useRouter } from 'vue-router';
3
- export { CreateVueRouter, FileModules, MenuItem, RouteModule } from './types.js';
4
- export { asyncGenerateRoute, createVueRouter, getRoutes, handleRouter } from './createVueRouter.js';
1
+ /**
2
+ * @Author : TanRui
3
+ * @WeChat : Tan578853789
4
+ * @File : @jnrs/vue-core/router
5
+ * @Date : 2025/11/05
6
+ * @Desc. : vue-router 封装
7
+ */
8
+ export type { RouteMeta, RouteLocationNormalizedGeneric, RouteLocationNormalizedLoadedGeneric } from 'vue-router';
9
+ export type { FileModules } from './types';
10
+ export { useRouter, useRoute } from 'vue-router';
11
+ export { createVueRouter, asyncGenerateRoute, handleRouter, getRoutes } from './createVueRouter';
@@ -1,5 +1,105 @@
1
- export { useRoute, useRouter } from 'vue-router';
2
- export { asyncGenerateRoute, createVueRouter, getRoutes, handleRouter } from './createVueRouter.js';
3
-
4
- console.log('%c✨ 欢迎使用 @jnrs/vue-core/router', 'background: #42B883; color: #39495C; font-weight: bold; padding: 4px 8px; border-radius: 4px; font-family: "Helvetica Neue", sans-serif;');
5
- //# sourceMappingURL=index.js.map
1
+ import { createWebHistory as l, createRouter as p } from "vue-router";
2
+ import { useRoute as w, useRouter as b } from "vue-router";
3
+ const d = {
4
+ history: l(),
5
+ routes: []
6
+ };
7
+ let n, s, i, m;
8
+ const R = ({
9
+ options: o,
10
+ fileModules: t,
11
+ layoutName: e,
12
+ globalComponent: a,
13
+ handleBeforeEach: r
14
+ }) => (s = t, e && (i = e), a && (m = a), n = p({ ...d, ...o }), n.beforeEach(
15
+ async (c, u) => r?.(c, u)
16
+ ), n), O = async (o, t = "push") => {
17
+ try {
18
+ return await n[t](o);
19
+ } catch (e) {
20
+ console.warn(`router.${t} 失败`, {
21
+ code: "ROUTER_NOT_MATCH",
22
+ message: "未匹配到有效路由",
23
+ error: o || e,
24
+ timestamp: (/* @__PURE__ */ new Date()).toISOString()
25
+ }), n[t]({ name: "404" });
26
+ }
27
+ }, h = (o) => s[`/src/views${o}.vue`], f = async (o, t) => {
28
+ for (const e of o) {
29
+ if (e.meta.fullPathTitle = t ? t.meta.fullPathTitle + "," + e.meta.title : e.meta.title, !e.component) {
30
+ e.children && e.children.length > 0 && f(e.children, e);
31
+ continue;
32
+ }
33
+ const a = e.component.replace(/[^a-zA-Z0-9/._-]/g, "");
34
+ if (!a.startsWith("/") || a.includes("..")) {
35
+ console.warn("[Router] 组件加载失败", {
36
+ code: "INVALID_COMPONENT_PATH",
37
+ message: "组件路径拼写不符合规则,应以 '/' 开头且不能包含 '..',请检查 component 字段是否配置正确",
38
+ menu: {
39
+ path: e.path,
40
+ name: e.name,
41
+ component: e.component
42
+ },
43
+ timestamp: (/* @__PURE__ */ new Date()).toISOString()
44
+ });
45
+ continue;
46
+ }
47
+ const r = h(a);
48
+ if (!r) {
49
+ console.warn("[Router] 组件加载失败", {
50
+ code: "COMPONENT_NOT_FOUND",
51
+ message: "组件加载器无效,请检查 component 字段是否配置正确",
52
+ menu: {
53
+ path: e.path,
54
+ name: e.name,
55
+ component: e.component
56
+ },
57
+ timestamp: (/* @__PURE__ */ new Date()).toISOString()
58
+ });
59
+ continue;
60
+ }
61
+ let c = {
62
+ name: e.name,
63
+ path: e.path,
64
+ meta: e.meta,
65
+ redirect: e.redirect,
66
+ component: () => r()
67
+ };
68
+ e.meta?.global ? (m !== void 0 && (c = {
69
+ name: "_globalComponent_" + e.name,
70
+ path: e.path,
71
+ meta: e.meta,
72
+ redirect: e.redirect,
73
+ component: () => m,
74
+ children: [
75
+ {
76
+ path: "",
77
+ name: e.name,
78
+ meta: e.meta,
79
+ component: () => r()
80
+ }
81
+ ]
82
+ }), n.addRoute(c)) : n.addRoute(i, c);
83
+ }
84
+ }, _ = () => {
85
+ const o = [];
86
+ for (const t of n.getRoutes())
87
+ o.push({
88
+ name: t.name,
89
+ path: t.path,
90
+ meta: t.meta
91
+ });
92
+ return o;
93
+ };
94
+ console.log(
95
+ "%c✨ 欢迎使用 @jnrs/vue-core/router",
96
+ 'background: #42B883; color: #39495C; font-weight: bold; padding: 4px 8px; border-radius: 4px; font-family: "Helvetica Neue", sans-serif;'
97
+ );
98
+ export {
99
+ f as asyncGenerateRoute,
100
+ R as createVueRouter,
101
+ _ as getRoutes,
102
+ O as handleRouter,
103
+ w as useRoute,
104
+ b as useRouter
105
+ };
@@ -1,5 +1,4 @@
1
- import { RouterOptions, RouteRecordRaw, RouteLocationNormalizedGeneric, RouteMeta } from 'vue-router';
2
-
1
+ import { RouteRecordRaw, RouterOptions, RouteLocationNormalizedGeneric } from 'vue-router';
3
2
  declare module 'vue-router' {
4
3
  interface RouteMeta {
5
4
  uuid?: string;
@@ -10,25 +9,14 @@ declare module 'vue-router' {
10
9
  fullPathTitle?: string;
11
10
  }
12
11
  }
13
- interface MenuItem {
14
- meta: RouteMeta;
15
- path: string;
16
- name?: string;
17
- todoCount?: number;
18
- component?: string;
19
- redirect?: string;
20
- children?: MenuItem[];
21
- }
22
- interface RouteModule {
12
+ export interface RouteModule {
23
13
  default: RouteRecordRaw | RouteRecordRaw[];
24
14
  }
25
- type FileModules = Record<string, () => Promise<RouteModule>>;
26
- interface CreateVueRouter<GC = unknown> {
15
+ export type FileModules = Record<string, () => Promise<RouteModule>>;
16
+ export interface CreateVueRouter<GC = unknown> {
27
17
  options: Partial<RouterOptions>;
28
18
  fileModules: FileModules;
29
19
  layoutName?: string;
30
20
  globalComponent?: Promise<GC>;
31
21
  handleBeforeEach?: (to: RouteLocationNormalizedGeneric, from: RouteLocationNormalizedGeneric) => void;
32
22
  }
33
-
34
- export type { CreateVueRouter, FileModules, MenuItem, RouteModule };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jnrs/vue-core",
3
- "version": "1.0.6",
3
+ "version": "1.0.7",
4
4
  "description": "As the name suggests.",
5
5
  "author": "Talia-Tan",
6
6
  "license": "ISC",
@@ -28,22 +28,32 @@
28
28
  "./router": {
29
29
  "import": "./dist/vueRouter/index.js",
30
30
  "types": "./dist/vueRouter/index.d.ts"
31
+ },
32
+ "./pinia": {
33
+ "import": "./dist/piniaStore/index.js",
34
+ "types": "./dist/piniaStore/index.d.ts"
31
35
  }
32
36
  },
33
- "dependencies": {
34
- "vue-router": "^4.5.1"
37
+ "peerDependencies": {
38
+ "pinia": "^3.0.3",
39
+ "pinia-plugin-persistedstate": "^4.7.1",
40
+ "vue": "^3.5.22",
41
+ "vue-router": "^4.5.1",
42
+ "@jnrs/core": "1.0.22",
43
+ "@jnrs/shared": "1.0.18"
35
44
  },
36
45
  "devDependencies": {
37
- "@rollup/plugin-node-resolve": "^16.0.3",
38
- "@rollup/plugin-typescript": "^12.3.0",
39
- "rollup": "^4.53.2",
40
- "rollup-plugin-dts": "^6.2.3",
46
+ "@vitejs/plugin-vue": "^6.0.1",
41
47
  "rimraf": "^6.0.1",
42
- "typescript": "^5.9.3"
48
+ "typescript": "^5.9.3",
49
+ "vite": "^7.1.7",
50
+ "vite-plugin-dts": "^4.5.4",
51
+ "vue-tsc": "^3.1.0"
43
52
  },
44
53
  "scripts": {
45
- "dev": "rollup -c -w",
46
- "build": "rimraf dist && rollup -c",
47
- "release": "node ./scripts/release.mjs"
54
+ "dev": "vite build --watch",
55
+ "build": "vue-tsc --build && vite build",
56
+ "release": "node ./scripts/release.mjs",
57
+ "type-check": "vue-tsc --build"
48
58
  }
49
59
  }
package/dist/index.cjs DELETED
@@ -1,8 +0,0 @@
1
- 'use strict';
2
-
3
- var createVueRouter = require('./vueRouter/createVueRouter.cjs');
4
-
5
- console.log('%c✨ 欢迎使用 @jnrs/vue-core', 'background: #42B883; color: #39495C; font-weight: bold; padding: 4px 8px; border-radius: 4px; font-family: "Helvetica Neue", sans-serif;');
6
-
7
- exports.router = createVueRouter;
8
- //# sourceMappingURL=index.cjs.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"index.cjs","sources":["../src/index.ts"],"sourcesContent":[null],"names":[],"mappings":";;;;AACA,OAAO,CAAC,GAAG,CACT,yBAAyB,EACzB,0IAA0I,CAC3I;;;;"}
package/dist/index.js.map DELETED
@@ -1 +0,0 @@
1
- {"version":3,"file":"index.js","sources":["../src/index.ts"],"sourcesContent":[null],"names":[],"mappings":";;;AACA,OAAO,CAAC,GAAG,CACT,yBAAyB,EACzB,0IAA0I,CAC3I"}
@@ -1,158 +0,0 @@
1
- 'use strict';
2
-
3
- var vueRouter = require('vue-router');
4
- var defaults = require('./defaults.cjs');
5
-
6
- let router;
7
- let _fileModules;
8
- let _layoutName;
9
- let _globalComponent;
10
- /**
11
- * 创建 vue-router 实例
12
- * @param options 配置项
13
- * @param fileModules 文件模块
14
- * @param layoutName 布局组件名称
15
- * @param globalComponent 全局 Layout 组件(mate.global=true时需传入)
16
- * @param handleBeforeEach 路由前置守卫处理函数
17
- * @returns router 实例
18
- */
19
- const createVueRouter = ({ options, fileModules, layoutName, globalComponent, handleBeforeEach }) => {
20
- _fileModules = fileModules;
21
- if (layoutName) {
22
- _layoutName = layoutName;
23
- }
24
- if (globalComponent) {
25
- _globalComponent = globalComponent;
26
- }
27
- router = vueRouter.createRouter({ ...defaults.DEFAULT_OPTIONS, ...options });
28
- router.beforeEach(async (to, from) => handleBeforeEach?.(to, from));
29
- return router;
30
- };
31
- /**
32
- * 路由跳转或替换
33
- * @param to 路由跳转参数
34
- * @param type 默认为push, push | replace
35
- * @returns Promise<boolean | void>
36
- */
37
- const handleRouter = async (to, type = 'push') => {
38
- try {
39
- const failure = await router[type](to);
40
- return failure;
41
- }
42
- catch (e) {
43
- console.warn(`router.${type} 失败`, {
44
- code: 'ROUTER_NOT_MATCH',
45
- message: '未匹配到有效路由',
46
- error: to || e,
47
- timestamp: new Date().toISOString()
48
- });
49
- router[type]({ name: '404' });
50
- }
51
- };
52
- /**
53
- * 组件加载器
54
- * @param safePath 安全处理后的组件路径
55
- * @param fileModules 文件模块
56
- * @returns
57
- */
58
- const getComponentLoader = (safePath) => {
59
- return _fileModules[`/src/views${safePath}.vue`];
60
- };
61
- /**
62
- * 设置动态路由,生成路由记录
63
- * @param menus 菜单项
64
- * @param parent 父级菜单项
65
- */
66
- const asyncGenerateRoute = async (menus, parent) => {
67
- for (const menu of menus) {
68
- menu.meta.fullPathTitle = parent
69
- ? parent.meta.fullPathTitle + ',' + menu.meta.title
70
- : menu.meta.title;
71
- // 当前项若没有component,则为目录,直接递归处理
72
- if (!menu.component) {
73
- if (menu.children && menu.children.length > 0) {
74
- asyncGenerateRoute(menu.children, menu);
75
- }
76
- continue;
77
- }
78
- // 安全处理组件路径
79
- const safePath = menu.component.replace(/[^a-zA-Z0-9/._-]/g, '');
80
- if (!safePath.startsWith('/') || safePath.includes('..')) {
81
- console.warn('[Router] 组件加载失败', {
82
- code: 'INVALID_COMPONENT_PATH',
83
- message: `组件路径拼写不符合规则,应以 '/' 开头且不能包含 '..',请检查 component 字段是否配置正确`,
84
- menu: {
85
- path: menu.path,
86
- name: menu.name,
87
- component: menu.component
88
- },
89
- timestamp: new Date().toISOString()
90
- });
91
- continue;
92
- }
93
- // 获取组件加载器
94
- const loader = getComponentLoader(safePath);
95
- if (!loader) {
96
- console.warn('[Router] 组件加载失败', {
97
- code: 'COMPONENT_NOT_FOUND',
98
- message: `组件加载器无效,请检查 component 字段是否配置正确`,
99
- menu: {
100
- path: menu.path,
101
- name: menu.name,
102
- component: menu.component
103
- },
104
- timestamp: new Date().toISOString()
105
- });
106
- continue;
107
- }
108
- // 根据 global 字段决定添加为全局路由还是布局子路由
109
- let routeRecord = {
110
- name: menu.name,
111
- path: menu.path,
112
- meta: menu.meta,
113
- redirect: menu.redirect,
114
- component: () => loader()
115
- };
116
- if (menu.meta?.global) {
117
- if (_globalComponent !== undefined) {
118
- routeRecord = {
119
- name: '_globalComponent_' + menu.name,
120
- path: menu.path,
121
- meta: menu.meta,
122
- redirect: menu.redirect,
123
- component: () => _globalComponent,
124
- children: [
125
- {
126
- path: '',
127
- name: menu.name,
128
- meta: menu.meta,
129
- component: () => loader()
130
- }
131
- ]
132
- };
133
- }
134
- router.addRoute(routeRecord);
135
- }
136
- else {
137
- router.addRoute(_layoutName, routeRecord);
138
- }
139
- }
140
- };
141
- // 获取路由
142
- const getRoutes = () => {
143
- const temp = [];
144
- for (const route of router.getRoutes()) {
145
- temp.push({
146
- name: route.name,
147
- path: route.path,
148
- meta: route.meta
149
- });
150
- }
151
- return temp;
152
- };
153
-
154
- exports.asyncGenerateRoute = asyncGenerateRoute;
155
- exports.createVueRouter = createVueRouter;
156
- exports.getRoutes = getRoutes;
157
- exports.handleRouter = handleRouter;
158
- //# sourceMappingURL=createVueRouter.cjs.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"createVueRouter.cjs","sources":["../../src/vueRouter/createVueRouter.ts"],"sourcesContent":[null],"names":["createRouter","DEFAULT_OPTIONS"],"mappings":";;;;;AAUA,IAAI,MAAc;AAClB,IAAI,YAAyB;AAC7B,IAAI,WAAmB;AACvB,IAAI,gBAAkC;AAEtC;;;;;;;;AAQG;AACH,MAAM,eAAe,GAAG,CAAC,EACvB,OAAO,EACP,WAAW,EACX,UAAU,EACV,eAAe,EACf,gBAAgB,EACA,KAAI;IACpB,YAAY,GAAG,WAAW;IAC1B,IAAI,UAAU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AAC;AACb,SAAA,CAAC;IACJ;AACA,IAAA,OAAO,IAAI;AACb;;;;;;;"}
@@ -1,153 +0,0 @@
1
- import { createRouter } from 'vue-router';
2
- import { DEFAULT_OPTIONS } from './defaults.js';
3
-
4
- let router;
5
- let _fileModules;
6
- let _layoutName;
7
- let _globalComponent;
8
- /**
9
- * 创建 vue-router 实例
10
- * @param options 配置项
11
- * @param fileModules 文件模块
12
- * @param layoutName 布局组件名称
13
- * @param globalComponent 全局 Layout 组件(mate.global=true时需传入)
14
- * @param handleBeforeEach 路由前置守卫处理函数
15
- * @returns router 实例
16
- */
17
- const createVueRouter = ({ options, fileModules, layoutName, globalComponent, handleBeforeEach }) => {
18
- _fileModules = fileModules;
19
- if (layoutName) {
20
- _layoutName = layoutName;
21
- }
22
- if (globalComponent) {
23
- _globalComponent = globalComponent;
24
- }
25
- router = createRouter({ ...DEFAULT_OPTIONS, ...options });
26
- router.beforeEach(async (to, from) => handleBeforeEach?.(to, from));
27
- return router;
28
- };
29
- /**
30
- * 路由跳转或替换
31
- * @param to 路由跳转参数
32
- * @param type 默认为push, push | replace
33
- * @returns Promise<boolean | void>
34
- */
35
- const handleRouter = async (to, type = 'push') => {
36
- try {
37
- const failure = await router[type](to);
38
- return failure;
39
- }
40
- catch (e) {
41
- console.warn(`router.${type} 失败`, {
42
- code: 'ROUTER_NOT_MATCH',
43
- message: '未匹配到有效路由',
44
- error: to || e,
45
- timestamp: new Date().toISOString()
46
- });
47
- router[type]({ name: '404' });
48
- }
49
- };
50
- /**
51
- * 组件加载器
52
- * @param safePath 安全处理后的组件路径
53
- * @param fileModules 文件模块
54
- * @returns
55
- */
56
- const getComponentLoader = (safePath) => {
57
- return _fileModules[`/src/views${safePath}.vue`];
58
- };
59
- /**
60
- * 设置动态路由,生成路由记录
61
- * @param menus 菜单项
62
- * @param parent 父级菜单项
63
- */
64
- const asyncGenerateRoute = async (menus, parent) => {
65
- for (const menu of menus) {
66
- menu.meta.fullPathTitle = parent
67
- ? parent.meta.fullPathTitle + ',' + menu.meta.title
68
- : menu.meta.title;
69
- // 当前项若没有component,则为目录,直接递归处理
70
- if (!menu.component) {
71
- if (menu.children && menu.children.length > 0) {
72
- asyncGenerateRoute(menu.children, menu);
73
- }
74
- continue;
75
- }
76
- // 安全处理组件路径
77
- const safePath = menu.component.replace(/[^a-zA-Z0-9/._-]/g, '');
78
- if (!safePath.startsWith('/') || safePath.includes('..')) {
79
- console.warn('[Router] 组件加载失败', {
80
- code: 'INVALID_COMPONENT_PATH',
81
- message: `组件路径拼写不符合规则,应以 '/' 开头且不能包含 '..',请检查 component 字段是否配置正确`,
82
- menu: {
83
- path: menu.path,
84
- name: menu.name,
85
- component: menu.component
86
- },
87
- timestamp: new Date().toISOString()
88
- });
89
- continue;
90
- }
91
- // 获取组件加载器
92
- const loader = getComponentLoader(safePath);
93
- if (!loader) {
94
- console.warn('[Router] 组件加载失败', {
95
- code: 'COMPONENT_NOT_FOUND',
96
- message: `组件加载器无效,请检查 component 字段是否配置正确`,
97
- menu: {
98
- path: menu.path,
99
- name: menu.name,
100
- component: menu.component
101
- },
102
- timestamp: new Date().toISOString()
103
- });
104
- continue;
105
- }
106
- // 根据 global 字段决定添加为全局路由还是布局子路由
107
- let routeRecord = {
108
- name: menu.name,
109
- path: menu.path,
110
- meta: menu.meta,
111
- redirect: menu.redirect,
112
- component: () => loader()
113
- };
114
- if (menu.meta?.global) {
115
- if (_globalComponent !== undefined) {
116
- routeRecord = {
117
- name: '_globalComponent_' + menu.name,
118
- path: menu.path,
119
- meta: menu.meta,
120
- redirect: menu.redirect,
121
- component: () => _globalComponent,
122
- children: [
123
- {
124
- path: '',
125
- name: menu.name,
126
- meta: menu.meta,
127
- component: () => loader()
128
- }
129
- ]
130
- };
131
- }
132
- router.addRoute(routeRecord);
133
- }
134
- else {
135
- router.addRoute(_layoutName, routeRecord);
136
- }
137
- }
138
- };
139
- // 获取路由
140
- const getRoutes = () => {
141
- const temp = [];
142
- for (const route of router.getRoutes()) {
143
- temp.push({
144
- name: route.name,
145
- path: route.path,
146
- meta: route.meta
147
- });
148
- }
149
- return temp;
150
- };
151
-
152
- export { asyncGenerateRoute, createVueRouter, getRoutes, handleRouter };
153
- //# sourceMappingURL=createVueRouter.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"createVueRouter.js","sources":["../../src/vueRouter/createVueRouter.ts"],"sourcesContent":[null],"names":[],"mappings":";;;AAUA,IAAI,MAAc;AAClB,IAAI,YAAyB;AAC7B,IAAI,WAAmB;AACvB,IAAI,gBAAkC;AAEtC;;;;;;;;AAQG;AACH,MAAM,eAAe,GAAG,CAAC,EACvB,OAAO,EACP,WAAW,EACX,UAAU,EACV,eAAe,EACf,gBAAgB,EACA,KAAI;IACpB,YAAY,GAAG,WAAW;IAC1B,IAAI,UAAU,EAAE;QACd,WAAW,GAAG,UAAU;IAC1B;IACA,IAAI,eAAe,EAAE;QACnB,gBAAgB,GAAG,eAAe;IACpC;IACA,MAAM,GAAG,YAAY,CAAC,EAAE,GAAG,eAAe,EAAE,GAAG,OAAO,EAAE,CAAC;AACzD,IAAA,MAAM,CAAC,UAAU,CACf,OAAO,EAAkC,EAAE,IAAoC,KAC7E,gBAAgB,GAAG,EAAE,EAAE,IAAI,CAAC,CAC/B;AACD,IAAA,OAAO,MAAM;AACf;AAEA;;;;;AAKG;AACH,MAAM,YAAY,GAAG,OAAO,EAAoB,EAAE,IAAA,GAA2B,MAAM,KAAI;AACrF,IAAA,IAAI;QACF,MAAM,OAAO,GAAG,MAAM,MAAM,CAAC,IAAI,CAAC,CAAC,EAAE,CAAC;AACtC,QAAA,OAAO,OAAO;IAChB;IAAE,OAAO,CAAC,EAAE;AACV,QAAA,OAAO,CAAC,IAAI,CAAC,CAAA,OAAA,EAAU,IAAI,KAAK,EAAE;AAChC,YAAA,IAAI,EAAE,kBAAkB;AACxB,YAAA,OAAO,EAAE,UAAU;YACnB,KAAK,EAAE,EAAE,IAAI,CAAC;AACd,YAAA,SAAS,EAAE,IAAI,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4BACf,IAAI,EAAE,IAAI,CAAC,IAAI;AACf,4BAAA,SAAS,EAAE,MAAM,MAAM;AACxB;AACF;iBACgB;YACrB;AACA,YAAA,MAAM,CAAC,QAAQ,CAAC,WAAW,CAAC;QAC9B;aAAO;AACL,YAAA,MAAM,CAAC,QAAQ,CAAC,WAAW,EAAE,WAAW,CAAC;QAC3C;IACF;AACF;AAEA;AACA,MAAM,SAAS,GAAG,MAAiB;IACjC,MAAM,IAAI,GAAe,EAAE;IAC3B,KAAK,MAAM,KAAK,IAAI,MAAM,CAAC,SAAS,EAAE,EAAE;QACtC,IAAI,CAAC,IAAI,CAAC;YACR,IAAI,EAAE,KAAK,CAAC,IAAc;YAC1B,IAAI,EAAE,KAAK,CAAC,IAAI;YAChB,IAAI,EAAE,KAAK,CAAC;AACb,SAAA,CAAC;IACJ;AACA,IAAA,OAAO,IAAI;AACb;;;;"}
@@ -1,16 +0,0 @@
1
- 'use strict';
2
-
3
- var vueRouter = require('vue-router');
4
-
5
- /**
6
- * 默认配置项
7
- * @property history - 路由历史模式
8
- * @property routes - 路由记录数组
9
- */
10
- const DEFAULT_OPTIONS = {
11
- history: vueRouter.createWebHistory(),
12
- routes: []
13
- };
14
-
15
- exports.DEFAULT_OPTIONS = DEFAULT_OPTIONS;
16
- //# sourceMappingURL=defaults.cjs.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"defaults.cjs","sources":["../../src/vueRouter/defaults.ts"],"sourcesContent":[null],"names":["createWebHistory"],"mappings":";;;;AAEA;;;;AAIG;AACH,MAAM,eAAe,GAAG;IACtB,OAAO,EAAEA,0BAAgB,EAAE;AAC3B,IAAA,MAAM,EAAE;;;;;"}
@@ -1,14 +0,0 @@
1
- import { createWebHistory } from 'vue-router';
2
-
3
- /**
4
- * 默认配置项
5
- * @property history - 路由历史模式
6
- * @property routes - 路由记录数组
7
- */
8
- const DEFAULT_OPTIONS = {
9
- history: createWebHistory(),
10
- routes: []
11
- };
12
-
13
- export { DEFAULT_OPTIONS };
14
- //# sourceMappingURL=defaults.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"defaults.js","sources":["../../src/vueRouter/defaults.ts"],"sourcesContent":[null],"names":[],"mappings":";;AAEA;;;;AAIG;AACH,MAAM,eAAe,GAAG;IACtB,OAAO,EAAE,gBAAgB,EAAE;AAC3B,IAAA,MAAM,EAAE;;;;;"}
@@ -1,20 +0,0 @@
1
- 'use strict';
2
-
3
- var vueRouter = require('vue-router');
4
- var createVueRouter = require('./createVueRouter.cjs');
5
-
6
- console.log('%c✨ 欢迎使用 @jnrs/vue-core/router', 'background: #42B883; color: #39495C; font-weight: bold; padding: 4px 8px; border-radius: 4px; font-family: "Helvetica Neue", sans-serif;');
7
-
8
- Object.defineProperty(exports, "useRoute", {
9
- enumerable: true,
10
- get: function () { return vueRouter.useRoute; }
11
- });
12
- Object.defineProperty(exports, "useRouter", {
13
- enumerable: true,
14
- get: function () { return vueRouter.useRouter; }
15
- });
16
- exports.asyncGenerateRoute = createVueRouter.asyncGenerateRoute;
17
- exports.createVueRouter = createVueRouter.createVueRouter;
18
- exports.getRoutes = createVueRouter.getRoutes;
19
- exports.handleRouter = createVueRouter.handleRouter;
20
- //# sourceMappingURL=index.cjs.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"index.cjs","sources":["../../src/vueRouter/index.ts"],"sourcesContent":[null],"names":[],"mappings":";;;;;AAWA,OAAO,CAAC,GAAG,CACT,gCAAgC,EAChC,0IAA0I,CAC3I;;;;;;;;;;;;;;;"}
@@ -1 +0,0 @@
1
- {"version":3,"file":"index.js","sources":["../../src/vueRouter/index.ts"],"sourcesContent":[null],"names":[],"mappings":";;;AAWA,OAAO,CAAC,GAAG,CACT,gCAAgC,EAChC,0IAA0I,CAC3I"}