@lytjs/router 6.4.0 → 6.5.0
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 +465 -0
- package/package.json +1 -1
package/README.md
ADDED
|
@@ -0,0 +1,465 @@
|
|
|
1
|
+
# @lytjs/router
|
|
2
|
+
|
|
3
|
+
> LytJS 声明式路由系统,支持嵌套路由、导航守卫和多种历史模式。
|
|
4
|
+
|
|
5
|
+
[](https://www.npmjs.com/package/@lytjs/router)
|
|
6
|
+
[](https://gitee.com/lytjs/lytjs/blob/main/LICENSE)
|
|
7
|
+
|
|
8
|
+
## 简介
|
|
9
|
+
|
|
10
|
+
`@lytjs/router` 是 LytJS 框架的官方路由管理器,提供声明式的路由配置、嵌套路由、路由懒加载、导航守卫等功能。它受到 Vue Router 的启发,但专为 LytJS 的响应式系统设计。
|
|
11
|
+
|
|
12
|
+
### 核心特性
|
|
13
|
+
|
|
14
|
+
- **声明式路由配置**:使用 JSON 风格的路由配置
|
|
15
|
+
- **嵌套路由**:支持多层级嵌套的路由结构
|
|
16
|
+
- **导航守卫**:提供完整的导航生命周期钩子
|
|
17
|
+
- **多种历史模式**:支持 Web History、Hash 模式和 Memory 模式
|
|
18
|
+
- **类型安全**:完整的 TypeScript 类型支持
|
|
19
|
+
- **路由懒加载**:支持组件和路由的按需加载
|
|
20
|
+
- **滚动行为控制**:细粒度的页面滚动位置管理
|
|
21
|
+
|
|
22
|
+
## 安装
|
|
23
|
+
|
|
24
|
+
```bash
|
|
25
|
+
npm install @lytjs/router
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
或使用 pnpm:
|
|
29
|
+
|
|
30
|
+
```bash
|
|
31
|
+
pnpm add @lytjs/router
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
## 依赖关系
|
|
35
|
+
|
|
36
|
+
`@lytjs/router` 依赖以下 LytJS 核心包:
|
|
37
|
+
|
|
38
|
+
- `@lytjs/reactivity` - 响应式系统
|
|
39
|
+
- `@lytjs/component` - 组件系统
|
|
40
|
+
- `@lytjs/vdom` - 虚拟 DOM
|
|
41
|
+
- `@lytjs/common-is` - 工具函数
|
|
42
|
+
- `@lytjs/common-env` - 环境检测
|
|
43
|
+
|
|
44
|
+
## 快速开始
|
|
45
|
+
|
|
46
|
+
### 创建路由器
|
|
47
|
+
|
|
48
|
+
```typescript
|
|
49
|
+
import { createRouter, createWebHistory } from '@lytjs/router';
|
|
50
|
+
import Home from './pages/Home';
|
|
51
|
+
import About from './pages/About';
|
|
52
|
+
import UserProfile from './pages/UserProfile';
|
|
53
|
+
|
|
54
|
+
const router = createRouter({
|
|
55
|
+
history: createWebHistory(),
|
|
56
|
+
routes: [
|
|
57
|
+
{
|
|
58
|
+
path: '/',
|
|
59
|
+
name: 'home',
|
|
60
|
+
component: Home
|
|
61
|
+
},
|
|
62
|
+
{
|
|
63
|
+
path: '/about',
|
|
64
|
+
name: 'about',
|
|
65
|
+
component: About
|
|
66
|
+
},
|
|
67
|
+
{
|
|
68
|
+
path: '/user/:id',
|
|
69
|
+
name: 'user-profile',
|
|
70
|
+
component: UserProfile,
|
|
71
|
+
props: true
|
|
72
|
+
}
|
|
73
|
+
]
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
export default router;
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
### 在应用中使用
|
|
80
|
+
|
|
81
|
+
```typescript
|
|
82
|
+
import { mount } from '@lytjs/vdom';
|
|
83
|
+
import App from './App';
|
|
84
|
+
import router from './router';
|
|
85
|
+
|
|
86
|
+
const app = mount(document.getElementById('app'), App, {
|
|
87
|
+
router
|
|
88
|
+
});
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
### 路由配置示例
|
|
92
|
+
|
|
93
|
+
```typescript
|
|
94
|
+
import { createRouter, createWebHistory } from '@lytjs/router';
|
|
95
|
+
import Layout from './components/Layout';
|
|
96
|
+
import Dashboard from './pages/Dashboard';
|
|
97
|
+
import Settings from './pages/Settings';
|
|
98
|
+
import UserList from './pages/UserList';
|
|
99
|
+
import UserDetail from './pages/UserDetail';
|
|
100
|
+
|
|
101
|
+
const router = createRouter({
|
|
102
|
+
history: createWebHistory(),
|
|
103
|
+
routes: [
|
|
104
|
+
{
|
|
105
|
+
path: '/',
|
|
106
|
+
component: Layout,
|
|
107
|
+
children: [
|
|
108
|
+
{
|
|
109
|
+
path: '',
|
|
110
|
+
redirect: '/dashboard'
|
|
111
|
+
},
|
|
112
|
+
{
|
|
113
|
+
path: 'dashboard',
|
|
114
|
+
name: 'dashboard',
|
|
115
|
+
component: Dashboard
|
|
116
|
+
},
|
|
117
|
+
{
|
|
118
|
+
path: 'settings',
|
|
119
|
+
name: 'settings',
|
|
120
|
+
component: Settings,
|
|
121
|
+
meta: { requiresAuth: true }
|
|
122
|
+
}
|
|
123
|
+
]
|
|
124
|
+
},
|
|
125
|
+
{
|
|
126
|
+
path: '/users',
|
|
127
|
+
component: UserList,
|
|
128
|
+
children: [
|
|
129
|
+
{
|
|
130
|
+
path: ':id',
|
|
131
|
+
name: 'user-detail',
|
|
132
|
+
component: UserDetail,
|
|
133
|
+
props: route => ({ id: route.params.id })
|
|
134
|
+
}
|
|
135
|
+
]
|
|
136
|
+
}
|
|
137
|
+
]
|
|
138
|
+
});
|
|
139
|
+
```
|
|
140
|
+
|
|
141
|
+
## 主要 API
|
|
142
|
+
|
|
143
|
+
### 路由器创建
|
|
144
|
+
|
|
145
|
+
#### `createRouter(options)`
|
|
146
|
+
|
|
147
|
+
创建路由器实例。
|
|
148
|
+
|
|
149
|
+
```typescript
|
|
150
|
+
import { createRouter } from '@lytjs/router';
|
|
151
|
+
|
|
152
|
+
const router = createRouter({
|
|
153
|
+
history: createWebHistory(),
|
|
154
|
+
routes: [],
|
|
155
|
+
scrollBehavior: (to, from, savedPosition) => {
|
|
156
|
+
if (savedPosition) {
|
|
157
|
+
return savedPosition;
|
|
158
|
+
}
|
|
159
|
+
return { top: 0 };
|
|
160
|
+
}
|
|
161
|
+
});
|
|
162
|
+
```
|
|
163
|
+
|
|
164
|
+
**选项说明:**
|
|
165
|
+
|
|
166
|
+
| 选项 | 类型 | 描述 |
|
|
167
|
+
|------|------|------|
|
|
168
|
+
| `history` | `RouterHistory` | 历史模式实例 |
|
|
169
|
+
| `routes` | `RouteRecordRaw[]` | 路由配置数组 |
|
|
170
|
+
| `scrollBehavior` | `RouterScrollBehavior` | 滚动行为函数 |
|
|
171
|
+
| `parseQuery` | `Function` | 自定义查询字符串解析 |
|
|
172
|
+
| `stringifyQuery` | `Function` | 自定义查询字符串序列化 |
|
|
173
|
+
|
|
174
|
+
#### `createWebHistory(base?)`
|
|
175
|
+
|
|
176
|
+
创建 HTML5 History 模式。
|
|
177
|
+
|
|
178
|
+
```typescript
|
|
179
|
+
import { createWebHistory } from '@lytjs/router';
|
|
180
|
+
|
|
181
|
+
const history = createWebHistory('/app');
|
|
182
|
+
```
|
|
183
|
+
|
|
184
|
+
#### `createWebHashHistory(base?)`
|
|
185
|
+
|
|
186
|
+
创建 Hash 模式路由。
|
|
187
|
+
|
|
188
|
+
```typescript
|
|
189
|
+
import { createWebHashHistory } from '@lytjs/router';
|
|
190
|
+
|
|
191
|
+
const history = createWebHashHistory('/app');
|
|
192
|
+
```
|
|
193
|
+
|
|
194
|
+
#### `createMemoryHistory(base?)`
|
|
195
|
+
|
|
196
|
+
创建 Memory 模式路由(适用于 SSR)。
|
|
197
|
+
|
|
198
|
+
```typescript
|
|
199
|
+
import { createMemoryHistory } from '@lytjs/router';
|
|
200
|
+
|
|
201
|
+
const history = createMemoryHistory();
|
|
202
|
+
```
|
|
203
|
+
|
|
204
|
+
### 路由守卫
|
|
205
|
+
|
|
206
|
+
```typescript
|
|
207
|
+
import { createRouter } from '@lytjs/router';
|
|
208
|
+
|
|
209
|
+
const router = createRouter({
|
|
210
|
+
history: createWebHistory(),
|
|
211
|
+
routes: []
|
|
212
|
+
});
|
|
213
|
+
|
|
214
|
+
router.beforeEach((to, from) => {
|
|
215
|
+
if (to.meta.requiresAuth && !isAuthenticated()) {
|
|
216
|
+
return { name: 'login', query: { redirect: to.fullPath } };
|
|
217
|
+
}
|
|
218
|
+
return true;
|
|
219
|
+
});
|
|
220
|
+
|
|
221
|
+
router.afterEach((to, from) => {
|
|
222
|
+
document.title = to.meta.title || 'LytJS App';
|
|
223
|
+
});
|
|
224
|
+
```
|
|
225
|
+
|
|
226
|
+
### 组合式函数
|
|
227
|
+
|
|
228
|
+
#### `useRouter()`
|
|
229
|
+
|
|
230
|
+
获取路由器实例。
|
|
231
|
+
|
|
232
|
+
```typescript
|
|
233
|
+
import { useRouter } from '@lytjs/router';
|
|
234
|
+
|
|
235
|
+
export function useUserProfile() {
|
|
236
|
+
const router = useRouter();
|
|
237
|
+
|
|
238
|
+
function goToSettings() {
|
|
239
|
+
router.push({ name: 'settings' });
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
function goBack() {
|
|
243
|
+
router.back();
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
return { goToSettings, goBack };
|
|
247
|
+
}
|
|
248
|
+
```
|
|
249
|
+
|
|
250
|
+
#### `useRoute()`
|
|
251
|
+
|
|
252
|
+
获取当前路由信息。
|
|
253
|
+
|
|
254
|
+
```typescript
|
|
255
|
+
import { useRoute } from '@lytjs/router';
|
|
256
|
+
|
|
257
|
+
export function UserProfile() {
|
|
258
|
+
const route = useRoute();
|
|
259
|
+
|
|
260
|
+
return () => (
|
|
261
|
+
<div>
|
|
262
|
+
<h1>用户 ID: {route.params.id}</h1>
|
|
263
|
+
<p>当前路径: {route.path}</p>
|
|
264
|
+
<p>查询参数: {JSON.stringify(route.query)}</p>
|
|
265
|
+
</div>
|
|
266
|
+
);
|
|
267
|
+
}
|
|
268
|
+
```
|
|
269
|
+
|
|
270
|
+
#### `useLink(options)`
|
|
271
|
+
|
|
272
|
+
创建链接点击处理器。
|
|
273
|
+
|
|
274
|
+
```typescript
|
|
275
|
+
import { useLink } from '@lytjs/router';
|
|
276
|
+
|
|
277
|
+
export function NavLink({ to, children }) {
|
|
278
|
+
const { navigate, isActive, href } = useLink({ to });
|
|
279
|
+
|
|
280
|
+
return () => (
|
|
281
|
+
<a
|
|
282
|
+
href={href}
|
|
283
|
+
class={isActive.value ? 'active' : ''}
|
|
284
|
+
onClick={(e) => {
|
|
285
|
+
e.preventDefault();
|
|
286
|
+
navigate();
|
|
287
|
+
}}
|
|
288
|
+
>
|
|
289
|
+
{children}
|
|
290
|
+
</a>
|
|
291
|
+
);
|
|
292
|
+
}
|
|
293
|
+
```
|
|
294
|
+
|
|
295
|
+
### 组件
|
|
296
|
+
|
|
297
|
+
#### `<RouterView>`
|
|
298
|
+
|
|
299
|
+
路由视图组件。
|
|
300
|
+
|
|
301
|
+
```typescript
|
|
302
|
+
import { RouterView } from '@lytjs/router';
|
|
303
|
+
|
|
304
|
+
export function AppLayout() {
|
|
305
|
+
return () => (
|
|
306
|
+
<div class="layout">
|
|
307
|
+
<nav>
|
|
308
|
+
<RouterLink to="/">首页</RouterLink>
|
|
309
|
+
<RouterLink to="/about">关于</RouterLink>
|
|
310
|
+
</nav>
|
|
311
|
+
<main>
|
|
312
|
+
<RouterView />
|
|
313
|
+
</main>
|
|
314
|
+
</div>
|
|
315
|
+
);
|
|
316
|
+
}
|
|
317
|
+
```
|
|
318
|
+
|
|
319
|
+
#### `<RouterLink>`
|
|
320
|
+
|
|
321
|
+
声明式导航链接。
|
|
322
|
+
|
|
323
|
+
```typescript
|
|
324
|
+
import { RouterLink } from '@lytjs/router';
|
|
325
|
+
|
|
326
|
+
export function Navigation() {
|
|
327
|
+
return () => (
|
|
328
|
+
<nav>
|
|
329
|
+
<RouterLink to="/" active-class="active">首页</RouterLink>
|
|
330
|
+
<RouterLink to="/about" active-class="active">关于</RouterLink>
|
|
331
|
+
<RouterLink to="/user/123" active-class="active">用户</RouterLink>
|
|
332
|
+
</nav>
|
|
333
|
+
);
|
|
334
|
+
}
|
|
335
|
+
```
|
|
336
|
+
|
|
337
|
+
### 导航方法
|
|
338
|
+
|
|
339
|
+
```typescript
|
|
340
|
+
import { useRouter } from '@lytjs/router';
|
|
341
|
+
|
|
342
|
+
const router = useRouter();
|
|
343
|
+
|
|
344
|
+
// 编程式导航
|
|
345
|
+
router.push('/home');
|
|
346
|
+
router.push({ name: 'user-profile', params: { id: '123' } });
|
|
347
|
+
router.push({ path: '/search', query: { q: 'keyword' } });
|
|
348
|
+
|
|
349
|
+
// 替换当前记录
|
|
350
|
+
router.replace('/dashboard');
|
|
351
|
+
|
|
352
|
+
// 前进/后退
|
|
353
|
+
router.back();
|
|
354
|
+
router.forward();
|
|
355
|
+
router.go(-1);
|
|
356
|
+
|
|
357
|
+
// 全屏导航
|
|
358
|
+
router.push('/full-page', { replace: false, force: true });
|
|
359
|
+
```
|
|
360
|
+
|
|
361
|
+
## 类型定义
|
|
362
|
+
|
|
363
|
+
### 路由记录
|
|
364
|
+
|
|
365
|
+
```typescript
|
|
366
|
+
interface RouteRecordRaw {
|
|
367
|
+
path: string;
|
|
368
|
+
name?: RouteRecordName;
|
|
369
|
+
component?: any;
|
|
370
|
+
components?: Record<string, any>;
|
|
371
|
+
redirect?: RouteLocationRaw;
|
|
372
|
+
props?: boolean | object | Function;
|
|
373
|
+
meta?: RouteMeta;
|
|
374
|
+
children?: RouteRecordRaw[];
|
|
375
|
+
beforeEnter?: NavigationGuard;
|
|
376
|
+
}
|
|
377
|
+
```
|
|
378
|
+
|
|
379
|
+
### 导航守卫
|
|
380
|
+
|
|
381
|
+
```typescript
|
|
382
|
+
type NavigationGuard = (
|
|
383
|
+
to: RouteLocationNormalized,
|
|
384
|
+
from: RouteLocationNormalized
|
|
385
|
+
) => NavigationGuardReturn;
|
|
386
|
+
|
|
387
|
+
type NavigationGuardReturn =
|
|
388
|
+
| void
|
|
389
|
+
| boolean
|
|
390
|
+
| string
|
|
391
|
+
| { name: string; params?: object; query?: object };
|
|
392
|
+
```
|
|
393
|
+
|
|
394
|
+
## 高级用法
|
|
395
|
+
|
|
396
|
+
### 路由懒加载
|
|
397
|
+
|
|
398
|
+
```typescript
|
|
399
|
+
import { createRouter } from '@lytjs/router';
|
|
400
|
+
|
|
401
|
+
const router = createRouter({
|
|
402
|
+
history: createWebHistory(),
|
|
403
|
+
routes: [
|
|
404
|
+
{
|
|
405
|
+
path: '/dashboard',
|
|
406
|
+
component: () => import('./pages/Dashboard')
|
|
407
|
+
},
|
|
408
|
+
{
|
|
409
|
+
path: '/settings',
|
|
410
|
+
component: () => import('./pages/Settings'),
|
|
411
|
+
meta: { requiresAuth: true }
|
|
412
|
+
}
|
|
413
|
+
]
|
|
414
|
+
});
|
|
415
|
+
```
|
|
416
|
+
|
|
417
|
+
### 滚动行为
|
|
418
|
+
|
|
419
|
+
```typescript
|
|
420
|
+
const router = createRouter({
|
|
421
|
+
history: createWebHistory(),
|
|
422
|
+
scrollBehavior(to, from, savedPosition) {
|
|
423
|
+
if (savedPosition) {
|
|
424
|
+
return savedPosition;
|
|
425
|
+
}
|
|
426
|
+
if (to.hash) {
|
|
427
|
+
return { el: to.hash };
|
|
428
|
+
}
|
|
429
|
+
return { top: 0, behavior: 'smooth' };
|
|
430
|
+
}
|
|
431
|
+
});
|
|
432
|
+
```
|
|
433
|
+
|
|
434
|
+
### 路由元信息
|
|
435
|
+
|
|
436
|
+
```typescript
|
|
437
|
+
interface RouteMeta {
|
|
438
|
+
requiresAuth?: boolean;
|
|
439
|
+
title?: string;
|
|
440
|
+
icon?: string;
|
|
441
|
+
roles?: string[];
|
|
442
|
+
}
|
|
443
|
+
|
|
444
|
+
const routes = [
|
|
445
|
+
{
|
|
446
|
+
path: '/admin',
|
|
447
|
+
component: AdminLayout,
|
|
448
|
+
meta: { requiresAuth: true, roles: ['admin'] },
|
|
449
|
+
children: [
|
|
450
|
+
{ path: 'users', component: UserManagement, meta: { title: '用户管理' } }
|
|
451
|
+
]
|
|
452
|
+
}
|
|
453
|
+
];
|
|
454
|
+
```
|
|
455
|
+
|
|
456
|
+
## 许可证
|
|
457
|
+
|
|
458
|
+
MIT License - [查看许可证](https://gitee.com/lytjs/lytjs/blob/main/LICENSE)
|
|
459
|
+
|
|
460
|
+
## 贡献指南
|
|
461
|
+
|
|
462
|
+
欢迎提交 Issue 和 Pull Request!
|
|
463
|
+
|
|
464
|
+
- [Gitee 仓库](https://gitee.com/lytjs/lytjs)
|
|
465
|
+
- [问题反馈](https://gitee.com/lytjs/lytjs/issues)
|
package/package.json
CHANGED