vitarx-router 4.0.0-beta.0 → 4.0.0-beta.1

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.
Files changed (88) hide show
  1. package/README.md +367 -81
  2. package/dist/components/RouterLink.d.ts +36 -35
  3. package/dist/components/RouterLink.js +38 -128
  4. package/dist/components/RouterView.d.ts +45 -14
  5. package/dist/components/RouterView.js +55 -42
  6. package/dist/core/common/constant.d.ts +41 -0
  7. package/dist/core/common/constant.js +42 -0
  8. package/dist/core/common/proxy.d.ts +9 -0
  9. package/dist/core/common/proxy.js +25 -0
  10. package/dist/core/common/resolve.d.ts +57 -0
  11. package/dist/core/common/resolve.js +178 -0
  12. package/dist/core/common/update.d.ts +8 -0
  13. package/dist/core/common/update.js +96 -0
  14. package/dist/core/common/utils.d.ts +70 -0
  15. package/dist/core/common/utils.js +131 -0
  16. package/dist/core/common/variable.d.ts +57 -0
  17. package/dist/core/common/variable.js +128 -0
  18. package/dist/core/index.d.ts +4 -6
  19. package/dist/core/index.js +3 -5
  20. package/dist/core/router/index.d.ts +4 -0
  21. package/dist/core/router/index.js +4 -0
  22. package/dist/core/router/manager.d.ts +273 -0
  23. package/dist/core/router/manager.js +612 -0
  24. package/dist/core/{router-memory.d.ts → router/memory.d.ts} +8 -14
  25. package/dist/core/{router-memory.js → router/memory.js} +16 -30
  26. package/dist/core/router/router.d.ts +363 -0
  27. package/dist/core/router/router.js +975 -0
  28. package/dist/core/router/web.d.ts +60 -0
  29. package/dist/core/router/web.js +213 -0
  30. package/dist/core/shared/hooks.d.ts +41 -0
  31. package/dist/core/shared/hooks.js +79 -0
  32. package/dist/core/shared/index.d.ts +7 -0
  33. package/dist/core/shared/index.js +7 -0
  34. package/dist/core/shared/inject.d.ts +26 -0
  35. package/dist/core/shared/inject.js +16 -0
  36. package/dist/core/shared/link.d.ts +67 -0
  37. package/dist/core/shared/link.js +173 -0
  38. package/dist/core/shared/navigation.d.ts +18 -0
  39. package/dist/core/shared/navigation.js +21 -0
  40. package/dist/core/shared/route.d.ts +39 -0
  41. package/dist/core/shared/route.js +27 -0
  42. package/dist/core/shared/router.d.ts +73 -0
  43. package/dist/core/shared/router.js +89 -0
  44. package/dist/core/shared/utils.d.ts +39 -0
  45. package/dist/core/shared/utils.js +59 -0
  46. package/dist/core/types/hooks.d.ts +54 -19
  47. package/dist/core/types/navigation.d.ts +64 -127
  48. package/dist/core/types/options.d.ts +43 -61
  49. package/dist/core/types/route.d.ts +166 -65
  50. package/dist/core/types/scroll.d.ts +12 -16
  51. package/dist/vite/auto-routes/handleHotUpdate.js +8 -16
  52. package/dist/vite/core/configUtils.d.ts +8 -7
  53. package/dist/vite/core/configUtils.js +24 -21
  54. package/dist/vite/core/generateRoutes.d.ts +7 -4
  55. package/dist/vite/core/generateRoutes.js +54 -42
  56. package/dist/vite/core/generateTypes.d.ts +1 -2
  57. package/dist/vite/core/generateTypes.js +95 -26
  58. package/dist/vite/core/logger.js +9 -8
  59. package/dist/vite/core/namingStrategy.d.ts +57 -0
  60. package/dist/vite/core/namingStrategy.js +106 -0
  61. package/dist/vite/core/parsePage.d.ts +8 -2
  62. package/dist/vite/core/parsePage.js +95 -26
  63. package/dist/vite/core/scanPages.js +21 -22
  64. package/dist/vite/core/types.d.ts +120 -25
  65. package/dist/vite/index.js +10 -4
  66. package/package.json +2 -2
  67. package/dist/core/constant.d.ts +0 -40
  68. package/dist/core/constant.js +0 -41
  69. package/dist/core/helpers.d.ts +0 -150
  70. package/dist/core/helpers.js +0 -83
  71. package/dist/core/normalize/component.d.ts +0 -8
  72. package/dist/core/normalize/component.js +0 -35
  73. package/dist/core/normalize/index.d.ts +0 -10
  74. package/dist/core/normalize/index.js +0 -36
  75. package/dist/core/normalize/inject-props.d.ts +0 -8
  76. package/dist/core/normalize/inject-props.js +0 -58
  77. package/dist/core/router-core.d.ts +0 -272
  78. package/dist/core/router-core.js +0 -569
  79. package/dist/core/router-history.d.ts +0 -85
  80. package/dist/core/router-history.js +0 -217
  81. package/dist/core/router-registry.d.ts +0 -133
  82. package/dist/core/router-registry.js +0 -421
  83. package/dist/core/router-types.d.ts +0 -1
  84. package/dist/core/router-types.js +0 -1
  85. package/dist/core/update.d.ts +0 -8
  86. package/dist/core/update.js +0 -61
  87. package/dist/core/utils.d.ts +0 -143
  88. package/dist/core/utils.js +0 -298
package/README.md CHANGED
@@ -7,6 +7,24 @@ Vitarx 前端框架官方路由解决方案,提供声明式路由配置、导
7
7
  [![GitHub](https://img.shields.io/badge/fork-999999?style=social&logo=github)](https://github.com/vitarx-lib/router)
8
8
  [![Documentation](https://img.shields.io/badge/docs-vitarx--router-blue)](https://router.vitarx.cn)
9
9
 
10
+ ## 目录
11
+
12
+ - [特性](#特性)
13
+ - [安装](#安装)
14
+ - [快速开始](#快速开始)
15
+ - [路由模式](#路由模式)
16
+ - [路由配置](#路由配置)
17
+ - [导航方法](#导航方法)
18
+ - [导航守卫](#导航守卫)
19
+ - [组件](#组件)
20
+ - [组合式 API](#组合式-api)
21
+ - [路由对象](#路由对象)
22
+ - [路由选项](#路由选项)
23
+ - [TypeScript 支持](#typescript-支持)
24
+ - [文件路由](#文件路由)
25
+ - [API 参考](#api-参考)
26
+ - [License](#license)
27
+
10
28
  ## 特性
11
29
 
12
30
  - 🚀 **多种路由模式** - 支持 Hash、History、Memory 三种路由模式
@@ -15,7 +33,9 @@ Vitarx 前端框架官方路由解决方案,提供声明式路由配置、导
15
33
  - 🔄 **动态路由** - 支持动态参数、正则约束、可选参数
16
34
  - 📦 **懒加载** - 内置组件懒加载支持
17
35
  - 🎯 **TypeScript** - 完整的类型支持
18
- - **滚动行为** - 可自定义路由切换时的滚动行为
36
+ - 📜 **滚动行为** - 可自定义路由切换时的滚动行为
37
+ - 🔄 **路由别名** - 支持多路径映射到同一路由
38
+ - 🎨 **命名视图** - 支持多视图布局
19
39
 
20
40
  ## 安装
21
41
 
@@ -117,9 +137,10 @@ location / {
117
137
  不修改 URL,适用于非浏览器环境或测试场景。
118
138
 
119
139
  ```typescript
120
- createRouter({
121
- routes,
122
- mode: 'memory'
140
+ import { createMemoryRouter } from 'vitarx-router'
141
+
142
+ createMemoryRouter({
143
+ routes
123
144
  })
124
145
  ```
125
146
 
@@ -141,6 +162,8 @@ const routes = defineRoutes(
141
162
 
142
163
  ### 动态路由
143
164
 
165
+ 支持动态参数、可选参数和正则约束:
166
+
144
167
  ```typescript
145
168
  const routes = defineRoutes(
146
169
  // 基本动态参数
@@ -158,6 +181,11 @@ const routes = defineRoutes(
158
181
  )
159
182
  ```
160
183
 
184
+ **参数说明:**
185
+ - `{id}` - 必填参数
186
+ - `{id?}` - 可选参数
187
+ - `pattern` - 正则约束,用于精确匹配
188
+
161
189
  ### 嵌套路由
162
190
 
163
191
  ```typescript
@@ -184,8 +212,27 @@ const routes = defineRoutes({
184
212
  router.push({ index: 'user-detail', params: { id: '123' } })
185
213
  ```
186
214
 
215
+ ### 路由别名
216
+
217
+ 支持单个或多个别名:
218
+
219
+ ```typescript
220
+ const routes = defineRoutes(
221
+ // 单个别名
222
+ { path: '/home', component: Home, alias: '/main' },
223
+
224
+ // 多个别名
225
+ { path: '/users', component: Users, alias: ['/members', '/people'] },
226
+
227
+ // 动态路由别名
228
+ { path: '/users/{id}', component: User, alias: '/members/{id}' }
229
+ )
230
+ ```
231
+
187
232
  ### 重定向
188
233
 
234
+ 支持字符串、对象和函数形式的重定向:
235
+
189
236
  ```typescript
190
237
  const routes = defineRoutes(
191
238
  // 字符串重定向
@@ -219,6 +266,8 @@ const routes = defineRoutes({
219
266
 
220
267
  ### 命名视图
221
268
 
269
+ 支持多个命名视图:
270
+
222
271
  ```jsx
223
272
  const routes = defineRoutes({
224
273
  path: '/settings',
@@ -229,12 +278,14 @@ const routes = defineRoutes({
229
278
  });
230
279
 
231
280
  // 模板中使用
232
- <RouterView />;
281
+ <RouterView />
233
282
  <RouterView name="sidebar" />
234
283
  ```
235
284
 
236
285
  ### 路由参数注入
237
286
 
287
+ 支持多种方式向组件注入参数:
288
+
238
289
  ```typescript
239
290
  const routes = defineRoutes(
240
291
  // 注入动态参数到组件 props
@@ -275,8 +326,16 @@ router.push({ index: '/search', query: { q: 'keyword' } })
275
326
 
276
327
  // 带 hash
277
328
  router.push({ index: '/about', hash: '#section' })
329
+
330
+ // 也支持直接传入路径字符串
331
+ router.push('/user/123')
332
+
333
+ // 或传入 RouteLocation 对象
334
+ router.push(routeLocation)
278
335
  ```
279
336
 
337
+ **返回值:** `Promise<NavigateResult>`
338
+
280
339
  ### replace
281
340
 
282
341
  替换当前路由,不添加历史记录。
@@ -285,6 +344,8 @@ router.push({ index: '/about', hash: '#section' })
285
344
  router.replace({ index: '/login' })
286
345
  ```
287
346
 
347
+ **返回值:** `Promise<NavigateResult>`
348
+
288
349
  ### go
289
350
 
290
351
  前进/后退指定步数。
@@ -305,6 +366,48 @@ router.back() // 等同于 router.go(-1)
305
366
  router.forward() // 等同于 router.go(1)
306
367
  ```
307
368
 
369
+ ### 导航结果
370
+
371
+ 所有导航方法返回 `NavigateResult` 对象:
372
+
373
+ ```typescript
374
+ interface NavigateResult {
375
+ state: NavState // 导航状态
376
+ message: string // 状态描述
377
+ to: RouteLocation | null // 目标路由
378
+ from: RouteLocation // 来源路由
379
+ redirectFrom?: RouteLocation // 重定向来源
380
+ }
381
+ ```
382
+
383
+ **导航状态枚举:**
384
+
385
+ | 状态 | 值 | 说明 |
386
+ |--------------|----|----------|
387
+ | `success` | 1 | 导航成功 |
388
+ | `aborted` | 2 | 导航被守卫阻止 |
389
+ | `cancelled` | 4 | 导航被新导航取消 |
390
+ | `duplicated` | 8 | 重复导航 |
391
+ | `notfound` | 16 | 路由未匹配 |
392
+
393
+ **检查导航状态:**
394
+
395
+ ```typescript
396
+ import { hasSuccess, hasState, NavState } from 'vitarx-router'
397
+
398
+ const result = await router.push({ index: '/home' })
399
+
400
+ // 检查是否成功
401
+ if (hasSuccess(result)) {
402
+ console.log('导航成功')
403
+ }
404
+
405
+ // 检查特定状态
406
+ if (hasState(result, NavState.aborted)) {
407
+ console.log('导航被阻止')
408
+ }
409
+ ```
410
+
308
411
  ---
309
412
 
310
413
  ## 导航守卫
@@ -329,6 +432,15 @@ router.beforeEach((to, from) => {
329
432
  })
330
433
  ```
331
434
 
435
+ **守卫返回值:**
436
+
437
+ | 返回值 | 说明 |
438
+ |------------------|----------|
439
+ | `true` / `void` | 继续导航 |
440
+ | `false` | 取消导航 |
441
+ | `string` | 重定向到指定路径 |
442
+ | `NavigateTarget` | 重定向到目标路由 |
443
+
332
444
  ### 全局后置守卫
333
445
 
334
446
  在路由跳转后执行,可用于更新页面标题等。
@@ -350,21 +462,72 @@ const routes = defineRoutes({
350
462
  if (!isAdmin()) {
351
463
  return { index: '/' }
352
464
  }
353
- },
354
- afterEnter: (to, from) => {
355
- console.log('进入管理页面')
356
465
  }
357
466
  })
358
467
  ```
359
468
 
360
- ### 守卫返回值
469
+ 支持数组形式的多个守卫:
361
470
 
362
- | 返回值 | 说明 |
363
- |------------------|----------|
364
- | `true` / `void` | 继续导航 |
365
- | `false` | 取消导航 |
366
- | `string` | 重定向到指定路径 |
367
- | `NavigateTarget` | 重定向到目标路由 |
471
+ ```typescript
472
+ const routes = defineRoutes({
473
+ path: '/admin',
474
+ component: Admin,
475
+ beforeEnter: [guard1, guard2]
476
+ })
477
+ ```
478
+
479
+ ### 组件内守卫
480
+
481
+ 在组件内注册离开守卫和更新钩子:
482
+
483
+ ```typescript
484
+ import { onBeforeRouteLeave, onBeforeRouteUpdate } from 'vitarx-router'
485
+
486
+ export default function UserComponent() {
487
+ // 离开守卫
488
+ onBeforeRouteLeave((to, from) => {
489
+ const answer = window.confirm('确定要离开吗?')
490
+ if (!answer) return false
491
+ })
492
+
493
+ // 更新钩子
494
+ onBeforeRouteUpdate((to, from) => {
495
+ console.log('路由参数更新', to.params)
496
+ })
497
+ }
498
+ ```
499
+
500
+ ### 错误处理
501
+
502
+ ```typescript
503
+ const router = createRouter({
504
+ routes,
505
+ onError: (error, to, from) => {
506
+ console.error('路由错误:', error)
507
+ }
508
+ })
509
+ ```
510
+
511
+ ### 404 处理
512
+
513
+ ```typescript
514
+ const router = createRouter({
515
+ routes,
516
+ onNotFound: (target) => {
517
+ console.log('未找到路由:', target)
518
+ return { index: '/404' } // 重定向到 404 页面
519
+ }
520
+ })
521
+ ```
522
+
523
+ 或使用 `missing` 组件:
524
+
525
+ ```typescript
526
+ const router = createRouter({
527
+ routes,
528
+ missing: NotFoundComponent
529
+ })
530
+ ```
368
531
 
369
532
  ---
370
533
 
@@ -381,16 +544,23 @@ const routes = defineRoutes({
381
544
  // 命名视图
382
545
  <RouterView name="sidebar" />
383
546
 
384
- // 带过渡效果
547
+ // 自定义渲染
385
548
  <RouterView>
386
- {(Component) => (
549
+ {(component, props, route) => (
387
550
  <Transition name="fade">
388
- <Component />
551
+ <Dynamic is={component} {...props} memo />
389
552
  </Transition>
390
553
  )}
391
554
  </RouterView>
392
555
  ```
393
556
 
557
+ **Props:**
558
+
559
+ | 属性 | 类型 | 默认值 | 说明 |
560
+ |------------|------------|-------------|---------|
561
+ | `name` | `string` | `'default'` | 命名视图名称 |
562
+ | `children` | `function` | - | 自定义渲染函数 |
563
+
394
564
  ### RouterLink
395
565
 
396
566
  声明式导航链接。
@@ -412,10 +582,37 @@ const routes = defineRoutes({
412
582
  // 替换模式
413
583
  <RouterLink to="/about" replace>关于</RouterLink>
414
584
 
415
- // 激活匹配
416
- <RouterLink to="/" active="strict">首页</RouterLink>
585
+ // 激活类名
586
+ <RouterLink
587
+ to="/"
588
+ activeClass="active"
589
+ exactActiveClass="exact-active"
590
+ >
591
+ 首页
592
+ </RouterLink>
593
+
594
+ // 禁用状态
595
+ <RouterLink to="/about" disabled>禁用链接</RouterLink>
596
+
597
+ // 外部链接
598
+ <RouterLink to="https://example.com" target="_blank">
599
+ 外部链接
600
+ </RouterLink>
417
601
  ```
418
602
 
603
+ **Props:**
604
+
605
+ | 属性 | 类型 | 默认值 | 说明 |
606
+ |--------------------|--------------------|----------|--------------------------|
607
+ | `to` | `string \| object` | - | 目标路由 |
608
+ | `replace` | `boolean` | `false` | 是否替换历史记录 |
609
+ | `disabled` | `boolean` | `false` | 是否禁用 |
610
+ | `activeClass` | `string` | - | 激活时应用的类名 |
611
+ | `exactActiveClass` | `string` | - | 精确匹配时应用的类名 |
612
+ | `ariaCurrentValue` | `string` | `'page'` | aria-current 属性值 |
613
+ | `callback` | `function` | - | 导航完成回调 |
614
+ | `viewTransition` | `boolean` | `false` | 是否使用 View Transition API |
615
+
419
616
  ---
420
617
 
421
618
  ## 组合式 API
@@ -461,6 +658,41 @@ export default function UserDetail() {
461
658
  }
462
659
  ```
463
660
 
661
+ **参数:**
662
+ - `global` - 是否返回全局路由对象,默认 `false`
663
+
664
+ ### useLink
665
+
666
+ 创建链接助手,用于自定义导航逻辑。
667
+
668
+ ```jsx
669
+ import { useLink } from 'vitarx-router'
670
+
671
+ export default function CustomLink({ to }) {
672
+ const { href, route, isActive, isExactActive, navigate } = useLink({ to })
673
+
674
+ return (
675
+ <a
676
+ href={href.value}
677
+ onClick={navigate}
678
+ class={{ active: isActive.value, 'exact-active': isExactActive.value }}
679
+ >
680
+ <slot />
681
+ </a>
682
+ )
683
+ }
684
+ ```
685
+
686
+ **返回值:**
687
+
688
+ | 属性 | 类型 | 说明 |
689
+ |-----------------|-----------------------------------|-------------|
690
+ | `href` | `Computed<string>` | 链接 href 属性值 |
691
+ | `route` | `Computed<RouteLocation \| null>` | 匹配的路由信息 |
692
+ | `isActive` | `Computed<boolean>` | 是否部分匹配当前路由 |
693
+ | `isExactActive` | `Computed<boolean>` | 是否精确匹配当前路由 |
694
+ | `navigate` | `function` | 执行导航 |
695
+
464
696
  ---
465
697
 
466
698
  ## 路由对象
@@ -469,28 +701,40 @@ export default function UserDetail() {
469
701
 
470
702
  当前路由信息对象。
471
703
 
472
- | 属性 | 类型 | 说明 |
473
- |------------|--------------------------|-------------------|
474
- | `path` | `string` | 路由路径 |
475
- | `fullPath` | `string` | 完整路径(含查询参数和 hash) |
476
- | `params` | `Record<string, any>` | 路由参数 |
477
- | `query` | `Record<string, string>` | 查询参数 |
478
- | `hash` | `string` | URL hash |
479
- | `name` | `string` | 路由名称 |
480
- | `meta` | `RouteMetaData` | 路由元数据 |
481
- | `matched` | `RouteNormalized[]` | 匹配的路由记录 |
482
-
483
- ### NavigateResult
484
-
485
- 导航结果对象。
486
-
487
- | 属性 | 类型 | 说明 |
488
- |----------------|-------------------------|-------|
489
- | `status` | `NavigateStatus` | 导航状态 |
490
- | `message` | `string` | 状态描述 |
491
- | `to` | `ReadonlyRouteLocation` | 目标路由 |
492
- | `from` | `ReadonlyRouteLocation` | 来源路由 |
493
- | `redirectFrom` | `NavigateTarget` | 重定向来源 |
704
+ ```typescript
705
+ interface RouteLocation {
706
+ href: string // 完整 href
707
+ path: RoutePath // 路由路径
708
+ hash: URLHash | '' // URL hash
709
+ params: URLParams // 路由参数
710
+ query: URLQuery // 查询参数
711
+ meta: RouteMetaData // 路由元数据
712
+ matched: RouteRecord[] // 匹配的路由记录
713
+ redirectFrom?: RouteLocation // 重定向来源
714
+ }
715
+ ```
716
+
717
+ ### RouteRecord
718
+
719
+ 解析后的路由记录。
720
+
721
+ ```typescript
722
+ interface RouteRecord {
723
+ isGroup: boolean // 是否为分组路由
724
+ parent?: RouteRecord // 父级路由记录
725
+ path: RoutePath // 路由路径
726
+ aliases?: RoutePath[] // 别名路径列表
727
+ name?: RouteName // 路由名称
728
+ meta?: RouteMetaData // 路由元数据
729
+ props?: NamedInjectProps // 参数注入配置
730
+ redirect?: Route['redirect'] // 重定向配置
731
+ component?: NamedRouteComponent // 视图组件
732
+ beforeEnter?: NavigationGuard[] // 路由守卫
733
+ pattern?: ResolvedPattern[] // 动态参数匹配模式
734
+ params?: URLParams // 路由参数
735
+ query?: URLQuery // 查询参数
736
+ }
737
+ ```
494
738
 
495
739
  ---
496
740
 
@@ -498,19 +742,32 @@ export default function UserDetail() {
498
742
 
499
743
  ### RouterOptions
500
744
 
501
- | 选项 | 类型 | 默认值 | 说明 |
502
- |------------------|--------------------------------------|------------|------------|
503
- | `routes` | `Route[]` | - | 路由配置数组(必填) |
504
- | `mode` | `'hash' \| 'path' \| 'memory'` | `'hash'` | 路由模式 |
505
- | `base` | `string` | `'/'` | 基础路径 |
506
- | `strict` | `boolean` | `false` | 是否区分大小写 |
507
- | `suffix` | `string \| string[] \| '*' \| false` | `'*'` | URL 后缀匹配 |
508
- | `defaultSuffix` | `string` | `''` | 默认后缀 |
509
- | `pattern` | `RegExp` | `/[\w.]+/` | 动态参数默认匹配模式 |
510
- | `scrollBehavior` | `ScrollBehavior \| Function` | `'auto'` | 滚动行为 |
511
- | `beforeEach` | `BeforeEnterCallback` | - | 全局前置守卫 |
512
- | `afterEach` | `AfterEnterCallback` | - | 全局后置守卫 |
513
- | `missing` | `RouteComponent` | - | 未匹配时渲染的组件 |
745
+ ```typescript
746
+ interface RouterOptions {
747
+ routes: Route[] | RouteManager // 路由配置(必填)
748
+ mode?: 'hash' | 'path' // 路由模式,默认 'hash'
749
+ base?: `/${string}` // 基础路径,默认 '/'
750
+ suffix?: `.${string}` // URL 后缀
751
+ props?: boolean | InjectPropsHandler // 全局 props 注入
752
+ scrollBehavior?: BeforeScrollCallback // 滚动行为
753
+ beforeEach?: NavigationGuard | NavigationGuard[] // 全局前置守卫
754
+ afterEach?: AfterCallback | AfterCallback[] // 全局后置守卫
755
+ onNotFound?: NotFoundHandler | NotFoundHandler[] // 404 处理
756
+ onError?: NavErrorListener | NavErrorListener[] // 错误处理
757
+ missing?: RouteViewComponent // 未匹配时渲染的组件
758
+ }
759
+ ```
760
+
761
+ ### RouteManagerOptions
762
+
763
+ ```typescript
764
+ interface RouteManagerOptions {
765
+ pattern?: RegExp // 动态参数默认匹配模式,默认 /[^/]+/
766
+ strict?: boolean // 是否严格匹配尾部斜杠,默认 false
767
+ ignoreCase?: boolean // 是否忽略大小写,默认 false
768
+ fallbackIndex?: boolean // 是否启用索引回退匹配,默认 false
769
+ }
770
+ ```
514
771
 
515
772
  ---
516
773
 
@@ -535,12 +792,13 @@ declare module 'vitarx-router' {
535
792
  declare module 'vitarx-router' {
536
793
  interface RouteIndexMap {
537
794
  'user-detail': { params: { id: string } }
538
- 'search': { query: { q: string } }
795
+ '/search': { query: { q: string } }
539
796
  }
540
797
  }
541
798
 
542
799
  // 使用时会有类型提示
543
800
  router.push({ index: 'user-detail', params: { id: '123' } })
801
+ router.push({ index: '/search', query: { q: 'keyword' } })
544
802
  ```
545
803
 
546
804
  ---
@@ -567,7 +825,7 @@ export default defineConfig({
567
825
  ### 使用生成的路由
568
826
 
569
827
  ```typescript
570
- import { routes } from 'vitarx-router/auto-routes'
828
+ import routes from 'virtual:vitarx-router:routes'
571
829
  import { createRouter } from 'vitarx-router'
572
830
 
573
831
  export const router = createRouter({ routes })
@@ -581,36 +839,64 @@ export const router = createRouter({ routes })
581
839
 
582
840
  ### 助手函数
583
841
 
584
- | 函数 | 说明 |
585
- |---------------------------------------|----------|
586
- | `createRouter(options)` | 创建路由器实例 |
587
- | `defineRoutes(...routes)` | 定义路由表 |
588
- | `defineRoute(route)` | 定义单个路由 |
589
- | `useRouter()` | 获取路由器实例 |
590
- | `useRoute()` | 获取当前路由信息 |
591
- | `hasNavigationStatus(result, status)` | 检查导航状态 |
842
+ | 函数 | 说明 |
843
+ |----------------------------------------|-----------------|
844
+ | `createRouter(options)` | 创建路由器实例 |
845
+ | `createWebRouter(options)` | 创建 Web 模式路由器 |
846
+ | `createMemoryRouter(options)` | 创建 Memory 模式路由器 |
847
+ | `createRouteManager(routes, options?)` | 创建路由管理器 |
848
+ | `defineRoutes(...routes)` | 定义路由表 |
849
+ | `useRouter()` | 获取路由器实例 |
850
+ | `useRoute(global?)` | 获取当前路由信息 |
851
+ | `useLink(options)` | 创建链接助手 |
852
+ | `onBeforeRouteLeave(guard)` | 注册离开守卫 |
853
+ | `onBeforeRouteUpdate(callback)` | 注册更新钩子 |
854
+ | `hasSuccess(result)` | 检查导航是否成功 |
855
+ | `hasState(result, status)` | 检查导航状态 |
592
856
 
593
857
  ### Router 实例方法
594
858
 
595
- | 方法 | 说明 |
596
- |--------------------|-----------|
597
- | `push(options)` | 跳转到新路由 |
598
- | `replace(options)` | 替换当前路由 |
599
- | `go(delta)` | 前进/后退指定步数 |
600
- | `back()` | 后退一步 |
601
- | `forward()` | 前进一步 |
602
- | `scrollTo(target)` | 滚动到指定位置 |
603
- | `initialize()` | 初始化路由器 |
859
+ | 方法 | 说明 |
860
+ |---------------------------------|-----------|
861
+ | `push(target)` | 跳转到新路由 |
862
+ | `replace(target)` | 替换当前路由 |
863
+ | `go(delta)` | 前进/后退指定步数 |
864
+ | `back()` | 后退一步 |
865
+ | `forward()` | 前进一步 |
866
+ | `addRoute(route, parent?)` | 添加路由 |
867
+ | `removeRoute(index)` | 移除路由 |
868
+ | `hasRoute(index)` | 检查路由是否存在 |
869
+ | `matchRoute(target)` | 匹配路由 |
870
+ | `buildUrl(path, query?, hash?)` | 构建 URL |
871
+ | `resolveComponents(route?)` | 解析异步组件 |
872
+ | `waitViewRender(navResult?)` | 等待视图渲染 |
873
+ | `isReady()` | 等待路由器就绪 |
874
+ | `beforeEach(guard)` | 添加前置守卫 |
875
+ | `afterEach(callback)` | 添加后置回调 |
876
+ | `destroy()` | 销毁路由器 |
604
877
 
605
878
  ### Router 实例属性
606
879
 
607
- | 属性 | 说明 |
608
- |------------------|---------------|
609
- | `route` | 当前路由信息(只读响应式) |
610
- | `options` | 路由器配置选项 |
611
- | `mode` | 路由模式 |
612
- | `isBrowser` | 是否浏览器环境 |
613
- | `scrollBehavior` | 滚动行为 |
880
+ | 属性 | 类型 | 说明 |
881
+ |-----------|---------------------------------|---------------|
882
+ | `route` | `ReadonlyObject<RouteLocation>` | 当前路由信息(只读响应式) |
883
+ | `routes` | `RouteRecord[]` | 所有注册的路由记录 |
884
+ | `config` | `ResolvedRouterConfig` | 路由器配置 |
885
+ | `manager` | `RouteManager` | 路由管理器实例 |
886
+
887
+ ### RouteManager 实例方法
888
+
889
+ | 方法 | 说明 |
890
+ |------------------------------|---------|
891
+ | `findByPath(path)` | 按路径查找路由 |
892
+ | `findByName(name)` | 按名称查找路由 |
893
+ | `find(index)` | 统一查找方法 |
894
+ | `matchByPath(path)` | 按路径匹配路由 |
895
+ | `matchByName(name, params?)` | 按名称匹配路由 |
896
+ | `match(index, params?)` | 统一匹配方法 |
897
+ | `addRoute(route, parent?)` | 添加路由 |
898
+ | `removeRoute(index)` | 移除路由 |
899
+ | `clearRoutes()` | 清空所有路由 |
614
900
 
615
901
  ---
616
902