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

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 (153) hide show
  1. package/README.md +135 -262
  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/file-router/config/configUtils.d.ts +54 -0
  52. package/dist/file-router/config/configUtils.js +88 -0
  53. package/dist/file-router/config/index.d.ts +6 -0
  54. package/dist/file-router/config/index.js +6 -0
  55. package/dist/{vite/core → file-router}/constants.d.ts +3 -3
  56. package/dist/{vite/core → file-router}/constants.js +3 -3
  57. package/dist/{vite/core → file-router/generator}/generateRoutes.d.ts +8 -36
  58. package/dist/file-router/generator/generateRoutes.js +214 -0
  59. package/dist/file-router/generator/generateTypes.d.ts +40 -0
  60. package/dist/file-router/generator/generateTypes.js +155 -0
  61. package/dist/file-router/generator/index.d.ts +7 -0
  62. package/dist/file-router/generator/index.js +7 -0
  63. package/dist/file-router/global.d.ts +57 -0
  64. package/dist/file-router/index.d.ts +143 -0
  65. package/dist/file-router/index.js +209 -0
  66. package/dist/file-router/macros/astValueExtractor.d.ts +15 -0
  67. package/dist/file-router/macros/astValueExtractor.js +236 -0
  68. package/dist/file-router/macros/definePage.d.ts +30 -0
  69. package/dist/file-router/macros/definePage.js +146 -0
  70. package/dist/file-router/macros/index.d.ts +6 -0
  71. package/dist/file-router/macros/index.js +6 -0
  72. package/dist/file-router/parser/exportChecker.d.ts +27 -0
  73. package/dist/file-router/parser/exportChecker.js +228 -0
  74. package/dist/file-router/parser/index.d.ts +6 -0
  75. package/dist/file-router/parser/index.js +6 -0
  76. package/dist/file-router/parser/parsePage.d.ts +12 -0
  77. package/dist/file-router/parser/parsePage.js +211 -0
  78. package/dist/file-router/scanner/filterUtils.d.ts +35 -0
  79. package/dist/file-router/scanner/filterUtils.js +188 -0
  80. package/dist/file-router/scanner/index.d.ts +8 -0
  81. package/dist/file-router/scanner/index.js +8 -0
  82. package/dist/file-router/scanner/routeTreeBuilder.d.ts +21 -0
  83. package/dist/file-router/scanner/routeTreeBuilder.js +312 -0
  84. package/dist/file-router/scanner/scanPages.d.ts +48 -0
  85. package/dist/file-router/scanner/scanPages.js +174 -0
  86. package/dist/file-router/types.d.ts +344 -0
  87. package/dist/file-router/types.js +1 -0
  88. package/dist/file-router/utils/babelUtils.d.ts +29 -0
  89. package/dist/file-router/utils/babelUtils.js +46 -0
  90. package/dist/file-router/utils/index.d.ts +10 -0
  91. package/dist/file-router/utils/index.js +10 -0
  92. package/dist/file-router/utils/logger.d.ts +54 -0
  93. package/dist/file-router/utils/logger.js +124 -0
  94. package/dist/file-router/utils/namingStrategy.d.ts +57 -0
  95. package/dist/file-router/utils/namingStrategy.js +106 -0
  96. package/dist/file-router/utils/pathUtils.d.ts +22 -0
  97. package/dist/file-router/utils/pathUtils.js +24 -0
  98. package/dist/file-router/utils/validateOptions.d.ts +26 -0
  99. package/dist/file-router/utils/validateOptions.js +233 -0
  100. package/dist/{vite → plugin-vite}/auto-routes/handleHotUpdate.js +8 -16
  101. package/dist/{vite → plugin-vite}/auto-routes/index.d.ts +0 -1
  102. package/dist/{vite → plugin-vite}/auto-routes/index.js +0 -1
  103. package/dist/plugin-vite/index.d.ts +24 -0
  104. package/dist/plugin-vite/index.js +94 -0
  105. package/package.json +23 -11
  106. package/dist/core/constant.d.ts +0 -40
  107. package/dist/core/constant.js +0 -41
  108. package/dist/core/helpers.d.ts +0 -150
  109. package/dist/core/helpers.js +0 -83
  110. package/dist/core/normalize/component.d.ts +0 -8
  111. package/dist/core/normalize/component.js +0 -35
  112. package/dist/core/normalize/index.d.ts +0 -10
  113. package/dist/core/normalize/index.js +0 -36
  114. package/dist/core/normalize/inject-props.d.ts +0 -8
  115. package/dist/core/normalize/inject-props.js +0 -58
  116. package/dist/core/router-core.d.ts +0 -272
  117. package/dist/core/router-core.js +0 -569
  118. package/dist/core/router-history.d.ts +0 -85
  119. package/dist/core/router-history.js +0 -217
  120. package/dist/core/router-registry.d.ts +0 -133
  121. package/dist/core/router-registry.js +0 -421
  122. package/dist/core/router-types.d.ts +0 -1
  123. package/dist/core/router-types.js +0 -1
  124. package/dist/core/update.d.ts +0 -8
  125. package/dist/core/update.js +0 -61
  126. package/dist/core/utils.d.ts +0 -143
  127. package/dist/core/utils.js +0 -298
  128. package/dist/vite/auto-routes/definePage.d.ts +0 -10
  129. package/dist/vite/auto-routes/definePage.js +0 -10
  130. package/dist/vite/core/babelUtils.d.ts +0 -16
  131. package/dist/vite/core/babelUtils.js +0 -28
  132. package/dist/vite/core/configUtils.d.ts +0 -76
  133. package/dist/vite/core/configUtils.js +0 -139
  134. package/dist/vite/core/generateRoutes.js +0 -233
  135. package/dist/vite/core/generateTypes.d.ts +0 -12
  136. package/dist/vite/core/generateTypes.js +0 -94
  137. package/dist/vite/core/index.d.ts +0 -40
  138. package/dist/vite/core/index.js +0 -46
  139. package/dist/vite/core/logger.d.ts +0 -23
  140. package/dist/vite/core/logger.js +0 -67
  141. package/dist/vite/core/parsePage.d.ts +0 -80
  142. package/dist/vite/core/parsePage.js +0 -906
  143. package/dist/vite/core/removeDefinePage.d.ts +0 -7
  144. package/dist/vite/core/removeDefinePage.js +0 -81
  145. package/dist/vite/core/scanPages.d.ts +0 -35
  146. package/dist/vite/core/scanPages.js +0 -536
  147. package/dist/vite/core/types.d.ts +0 -482
  148. package/dist/vite/core/validateOptions.d.ts +0 -29
  149. package/dist/vite/core/validateOptions.js +0 -140
  150. package/dist/vite/index.d.ts +0 -66
  151. package/dist/vite/index.js +0 -211
  152. /package/dist/{vite/core/types.js → file-router/global.js} +0 -0
  153. /package/dist/{vite → plugin-vite}/auto-routes/handleHotUpdate.d.ts +0 -0
package/README.md CHANGED
@@ -15,17 +15,14 @@ Vitarx 前端框架官方路由解决方案,提供声明式路由配置、导
15
15
  - 🔄 **动态路由** - 支持动态参数、正则约束、可选参数
16
16
  - 📦 **懒加载** - 内置组件懒加载支持
17
17
  - 🎯 **TypeScript** - 完整的类型支持
18
- - **滚动行为** - 可自定义路由切换时的滚动行为
18
+ - 📜 **滚动行为** - 可自定义路由切换时的滚动行为
19
+ - 🔄 **路由别名** - 支持多路径映射到同一路由
20
+ - 🎨 **命名视图** - 支持多视图布局
19
21
 
20
22
  ## 安装
21
23
 
22
24
  ```bash
23
- # 使用 npm
24
25
  npm install vitarx-router
25
- # 或者使用 yarn
26
- yarn add vitarx-router
27
- # 或者使用 pnpm
28
- pnpm add vitarx-router
29
26
  ```
30
27
 
31
28
  ## 快速开始
@@ -33,10 +30,9 @@ pnpm add vitarx-router
33
30
  ### 1. 创建路由配置
34
31
 
35
32
  ```typescript
36
- // src/router/index.ts
37
33
  import { createRouter, defineRoutes } from 'vitarx-router'
38
- import Home from '../pages/Home.jsx'
39
- import About from '../pages/About.jsx'
34
+ import Home from './pages/Home.jsx'
35
+ import About from './pages/About.jsx'
40
36
 
41
37
  const routes = defineRoutes(
42
38
  { path: '/', component: Home },
@@ -49,7 +45,6 @@ export const router = createRouter({ routes })
49
45
  ### 2. 注册路由器
50
46
 
51
47
  ```typescript
52
- // src/main.ts
53
48
  import { createApp } from 'vitarx'
54
49
  import { router } from './router'
55
50
  import App from './App.jsx'
@@ -60,7 +55,6 @@ createApp(App).use(router).mount('#app')
60
55
  ### 3. 使用路由视图
61
56
 
62
57
  ```jsx
63
- // App.jsx
64
58
  import { RouterView } from 'vitarx-router'
65
59
 
66
60
  export default function App() {
@@ -76,8 +70,6 @@ export default function App() {
76
70
  }
77
71
  ```
78
72
 
79
- ---
80
-
81
73
  ## 路由模式
82
74
 
83
75
  ### Hash 模式(默认)
@@ -89,7 +81,6 @@ createRouter({
89
81
  routes,
90
82
  mode: 'hash'
91
83
  })
92
- // URL: https://example.com/#/user/123
93
84
  ```
94
85
 
95
86
  ### History 模式
@@ -101,7 +92,6 @@ createRouter({
101
92
  routes,
102
93
  mode: 'path'
103
94
  })
104
- // URL: https://example.com/user/123
105
95
  ```
106
96
 
107
97
  **服务器配置(Nginx):**
@@ -117,13 +107,10 @@ location / {
117
107
  不修改 URL,适用于非浏览器环境或测试场景。
118
108
 
119
109
  ```typescript
120
- createRouter({
121
- routes,
122
- mode: 'memory'
123
- })
124
- ```
110
+ import { createMemoryRouter } from 'vitarx-router'
125
111
 
126
- ---
112
+ createMemoryRouter({ routes })
113
+ ```
127
114
 
128
115
  ## 路由配置
129
116
 
@@ -134,26 +121,22 @@ import { defineRoutes } from 'vitarx-router'
134
121
 
135
122
  const routes = defineRoutes(
136
123
  { path: '/', component: Home },
137
- { path: '/about', component: About },
138
- { path: '/contact', component: Contact }
124
+ { path: '/about', component: About }
139
125
  )
140
126
  ```
141
127
 
142
128
  ### 动态路由
143
129
 
130
+ 支持动态参数、可选参数和正则约束:
131
+
144
132
  ```typescript
145
133
  const routes = defineRoutes(
146
- // 基本动态参数
147
134
  { path: '/user/{id}', component: User },
148
-
149
- // 可选参数
150
135
  { path: '/search/{query?}', component: Search },
151
-
152
- // 带正则约束
153
136
  {
154
137
  path: '/post/{id}',
155
138
  component: Post,
156
- pattern: { id: /^\d+$/ } // 只匹配数字
139
+ pattern: { id: /^\d+$/ }
157
140
  }
158
141
  )
159
142
  ```
@@ -180,27 +163,24 @@ const routes = defineRoutes({
180
163
  component: UserDetail
181
164
  })
182
165
 
183
- // 导航时使用名称
184
166
  router.push({ index: 'user-detail', params: { id: '123' } })
185
167
  ```
186
168
 
169
+ ### 路由别名
170
+
171
+ ```typescript
172
+ const routes = defineRoutes(
173
+ { path: '/home', component: Home, alias: '/main' },
174
+ { path: '/users', component: Users, alias: ['/members', '/people'] }
175
+ )
176
+ ```
177
+
187
178
  ### 重定向
188
179
 
189
180
  ```typescript
190
181
  const routes = defineRoutes(
191
- // 字符串重定向
192
182
  { path: '/home', redirect: '/' },
193
-
194
- // 对象重定向
195
- { path: '/old-user/{id}', redirect: { index: 'user-detail' } },
196
-
197
- // 函数重定向
198
- {
199
- path: '/search/{query}',
200
- redirect: (to) => {
201
- return { index: 'search-results', query: { q: to.params.query } }
202
- }
203
- }
183
+ { path: '/old-user/{id}', redirect: { index: 'user-detail' } }
204
184
  )
205
185
  ```
206
186
 
@@ -219,7 +199,7 @@ const routes = defineRoutes({
219
199
 
220
200
  ### 命名视图
221
201
 
222
- ```jsx
202
+ ```tsx
223
203
  const routes = defineRoutes({
224
204
  path: '/settings',
225
205
  component: {
@@ -229,34 +209,10 @@ const routes = defineRoutes({
229
209
  });
230
210
 
231
211
  // 模板中使用
232
- <RouterView />;
212
+ <RouterView />
233
213
  <RouterView name="sidebar" />
234
214
  ```
235
215
 
236
- ### 路由参数注入
237
-
238
- ```typescript
239
- const routes = defineRoutes(
240
- // 注入动态参数到组件 props
241
- { path: '/user/{id}', component: User, props: true },
242
-
243
- // 注入静态对象
244
- { path: '/about', component: About, props: { showFooter: true } },
245
-
246
- // 使用函数动态注入
247
- {
248
- path: '/search/{query}',
249
- component: Search,
250
- props: (route) => ({
251
- query: route.params.query,
252
- page: route.query.page
253
- })
254
- }
255
- )
256
- ```
257
-
258
- ---
259
-
260
216
  ## 导航方法
261
217
 
262
218
  ### push
@@ -264,17 +220,10 @@ const routes = defineRoutes(
264
220
  跳转到新路由,添加历史记录。
265
221
 
266
222
  ```typescript
267
- // 路径导航
268
223
  router.push({ index: '/user/123' })
269
-
270
- // 命名导航
271
224
  router.push({ index: 'user-detail', params: { id: '123' } })
272
-
273
- // 带查询参数
274
225
  router.push({ index: '/search', query: { q: 'keyword' } })
275
-
276
- // 带 hash
277
- router.push({ index: '/about', hash: '#section' })
226
+ router.push('/user/123')
278
227
  ```
279
228
 
280
229
  ### replace
@@ -290,52 +239,56 @@ router.replace({ index: '/login' })
290
239
  前进/后退指定步数。
291
240
 
292
241
  ```typescript
293
- router.go(-1) // 后退一步
294
- router.go(1) // 前进一步
295
- router.go(2) // 前进两步
296
- router.go() // 刷新当前页面
242
+ router.go(-1)
243
+ router.go(1)
244
+ router.go(2)
297
245
  ```
298
246
 
299
247
  ### back / forward
300
248
 
301
- 后退/前进一步。
249
+ ```typescript
250
+ router.back()
251
+ router.forward()
252
+ ```
253
+
254
+ ### 导航结果
302
255
 
303
256
  ```typescript
304
- router.back() // 等同于 router.go(-1)
305
- router.forward() // 等同于 router.go(1)
257
+ import { hasSuccess, NavState } from 'vitarx-router'
258
+
259
+ const result = await router.push({ index: '/home' })
260
+
261
+ if (hasSuccess(result)) {
262
+ console.log('导航成功')
263
+ }
306
264
  ```
307
265
 
308
- ---
266
+ **导航状态:**
267
+
268
+ | 状态 | 值 | 说明 |
269
+ |--------------|----|----------|
270
+ | `success` | 1 | 导航成功 |
271
+ | `aborted` | 2 | 导航被守卫阻止 |
272
+ | `cancelled` | 4 | 导航被新导航取消 |
273
+ | `duplicated` | 8 | 重复导航 |
274
+ | `notfound` | 16 | 路由未匹配 |
309
275
 
310
276
  ## 导航守卫
311
277
 
312
278
  ### 全局前置守卫
313
279
 
314
- 在路由跳转前执行,可用于权限验证。
315
-
316
280
  ```typescript
317
- const router = createRouter({ routes })
318
-
319
281
  router.beforeEach((to, from) => {
320
- // 需要登录的页面
321
282
  if (to.meta.requiresAuth && !isAuthenticated()) {
322
- return { index: '/login' } // 重定向到登录页
283
+ return { index: '/login' }
323
284
  }
324
-
325
- // 返回 false 取消导航
326
- // return false
327
-
328
- // 返回 void 或 true 继续导航
329
285
  })
330
286
  ```
331
287
 
332
288
  ### 全局后置守卫
333
289
 
334
- 在路由跳转后执行,可用于更新页面标题等。
335
-
336
290
  ```typescript
337
291
  router.afterEach((to, from) => {
338
- // 更新页面标题
339
292
  document.title = to.meta.title || '默认标题'
340
293
  })
341
294
  ```
@@ -350,23 +303,25 @@ const routes = defineRoutes({
350
303
  if (!isAdmin()) {
351
304
  return { index: '/' }
352
305
  }
353
- },
354
- afterEnter: (to, from) => {
355
- console.log('进入管理页面')
356
306
  }
357
307
  })
358
308
  ```
359
309
 
360
- ### 守卫返回值
310
+ ### 组件内守卫
361
311
 
362
- | 返回值 | 说明 |
363
- |------------------|----------|
364
- | `true` / `void` | 继续导航 |
365
- | `false` | 取消导航 |
366
- | `string` | 重定向到指定路径 |
367
- | `NavigateTarget` | 重定向到目标路由 |
312
+ ```typescript
313
+ import { onBeforeRouteLeave, onBeforeRouteUpdate } from 'vitarx-router'
368
314
 
369
- ---
315
+ export default function UserComponent() {
316
+ onBeforeRouteLeave((to, from) => {
317
+ if (!window.confirm('确定要离开吗?')) return false
318
+ })
319
+
320
+ onBeforeRouteUpdate((to, from) => {
321
+ console.log('路由参数更新', to.params)
322
+ })
323
+ }
324
+ ```
370
325
 
371
326
  ## 组件
372
327
 
@@ -375,17 +330,12 @@ const routes = defineRoutes({
375
330
  渲染匹配的路由组件。
376
331
 
377
332
  ```jsx
378
- // 基本用法
379
333
  <RouterView />
380
-
381
- // 命名视图
382
334
  <RouterView name="sidebar" />
383
-
384
- // 带过渡效果
385
335
  <RouterView>
386
- {(Component) => (
336
+ {(component, props, route) => (
387
337
  <Transition name="fade">
388
- <Component />
338
+ <Dynamic is={component} {...props} memo />
389
339
  </Transition>
390
340
  )}
391
341
  </RouterView>
@@ -396,135 +346,89 @@ const routes = defineRoutes({
396
346
  声明式导航链接。
397
347
 
398
348
  ```jsx
399
- // 基本用法
400
349
  <RouterLink to="/">首页</RouterLink>
401
-
402
- // 命名路由
403
350
  <RouterLink to={{ index: 'user-detail', params: { id: '123' } }}>
404
351
  用户详情
405
352
  </RouterLink>
406
-
407
- // 带查询参数
408
- <RouterLink to={{ index: '/search', query: { q: 'keyword' } }}>
409
- 搜索
410
- </RouterLink>
411
-
412
- // 替换模式
413
353
  <RouterLink to="/about" replace>关于</RouterLink>
414
-
415
- // 激活匹配
416
- <RouterLink to="/" active="strict">首页</RouterLink>
354
+ <RouterLink to="/" activeClass="active" exactActiveClass="exact-active">
355
+ 首页
356
+ </RouterLink>
417
357
  ```
418
358
 
419
- ---
420
-
421
359
  ## 组合式 API
422
360
 
423
361
  ### useRouter
424
362
 
425
363
  获取路由器实例。
426
364
 
427
- ```jsx
365
+ ```typescript
428
366
  import { useRouter } from 'vitarx-router'
429
367
 
430
- export default function Component() {
431
- const router = useRouter()
432
-
433
- const handleClick = () => {
434
- router.push({ index: '/home' })
435
- }
436
-
437
- return <button onClick={handleClick}>跳转</button>
438
- }
368
+ const router = useRouter()
369
+ router.push({ index: '/home' })
439
370
  ```
440
371
 
441
372
  ### useRoute
442
373
 
443
374
  获取当前路由信息。
444
375
 
445
- ```jsx
376
+ ```typescript
446
377
  import { useRoute } from 'vitarx-router'
447
378
 
448
- export default function UserDetail() {
449
- const route = useRoute()
450
-
451
- // 路由参数
452
- const userId = route.params.id
453
-
454
- // 查询参数
455
- const query = route.query
456
-
457
- // 路由元数据
458
- const meta = route.meta
459
-
460
- return <div>用户ID: {userId}</div>
461
- }
379
+ const route = useRoute()
380
+ const userId = route.params.id
381
+ const query = route.query
462
382
  ```
463
383
 
464
- ---
384
+ ### useLink
465
385
 
466
- ## 路由对象
386
+ 创建链接助手。
467
387
 
468
- ### RouteLocation
388
+ ```typescript
389
+ import { useLink } from 'vitarx-router'
469
390
 
470
- 当前路由信息对象。
391
+ const { href, route, isActive, isExactActive, navigate } = useLink({ to: '/home' })
392
+ ```
471
393
 
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[]` | 匹配的路由记录 |
394
+ ## 文件路由
482
395
 
483
- ### NavigateResult
396
+ 配合 Vite 插件实现基于文件系统的路由自动生成。
484
397
 
485
- 导航结果对象。
398
+ ### 安装配置
486
399
 
487
- | 属性 | 类型 | 说明 |
488
- |----------------|-------------------------|-------|
489
- | `status` | `NavigateStatus` | 导航状态 |
490
- | `message` | `string` | 状态描述 |
491
- | `to` | `ReadonlyRouteLocation` | 目标路由 |
492
- | `from` | `ReadonlyRouteLocation` | 来源路由 |
493
- | `redirectFrom` | `NavigateTarget` | 重定向来源 |
400
+ ```typescript
401
+ import VitarxRouter from 'vitarx-router/vite'
494
402
 
495
- ---
403
+ export default defineConfig({
404
+ plugins: [
405
+ VitarxRouter({
406
+ pagesDir: 'src/pages'
407
+ })
408
+ ]
409
+ })
410
+ ```
496
411
 
497
- ## 路由选项
412
+ ### 使用生成的路由
498
413
 
499
- ### RouterOptions
414
+ ```typescript
415
+ import routes from 'virtual:vitarx-router:routes'
416
+ import { createRouter } from 'vitarx-router'
500
417
 
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` | - | 未匹配时渲染的组件 |
418
+ export const router = createRouter({ routes })
419
+ ```
514
420
 
515
- ---
421
+ 详细配置请参考 [Vite 插件文档](src/plugin-vite/README.md)。
516
422
 
517
423
  ## TypeScript 支持
518
424
 
519
425
  ### 扩展路由元数据类型
520
426
 
521
427
  ```typescript
522
- // 在全局类型声明文件中
523
428
  declare module 'vitarx-router' {
524
429
  interface RouteMetaData {
525
430
  title?: string
526
431
  requiresAuth?: boolean
527
- icon?: string
528
432
  }
529
433
  }
530
434
  ```
@@ -535,84 +439,53 @@ declare module 'vitarx-router' {
535
439
  declare module 'vitarx-router' {
536
440
  interface RouteIndexMap {
537
441
  'user-detail': { params: { id: string } }
538
- 'search': { query: { q: string } }
442
+ '/search': { query: { q: string } }
539
443
  }
540
444
  }
541
-
542
- // 使用时会有类型提示
543
- router.push({ index: 'user-detail', params: { id: '123' } })
544
445
  ```
545
446
 
546
- ---
547
-
548
- ## 文件路由
549
-
550
- 配合 Vite 插件实现基于文件系统的路由自动生成。
551
-
552
- ### 安装配置
553
-
554
- ```typescript
555
- // vite.config.ts
556
- import VitarxRouter from 'vitarx-router/vite'
557
-
558
- export default defineConfig({
559
- plugins: [
560
- VitarxRouter({
561
- pagesDir: 'src/pages'
562
- })
563
- ]
564
- })
565
- ```
566
-
567
- ### 使用生成的路由
568
-
569
- ```typescript
570
- import { routes } from 'vitarx-router/auto-routes'
571
- import { createRouter } from 'vitarx-router'
572
-
573
- export const router = createRouter({ routes })
574
- ```
575
-
576
- 详细配置请参考 [Vite 插件文档](./src/vite/README.md)。
577
-
578
- ---
579
-
580
447
  ## API 参考
581
448
 
582
449
  ### 助手函数
583
450
 
584
- | 函数 | 说明 |
585
- |---------------------------------------|----------|
586
- | `createRouter(options)` | 创建路由器实例 |
587
- | `defineRoutes(...routes)` | 定义路由表 |
588
- | `defineRoute(route)` | 定义单个路由 |
589
- | `useRouter()` | 获取路由器实例 |
590
- | `useRoute()` | 获取当前路由信息 |
591
- | `hasNavigationStatus(result, status)` | 检查导航状态 |
451
+ | 函数 | 说明 |
452
+ |----------------------------------------|-----------------|
453
+ | `createRouter(options)` | 创建路由器实例 |
454
+ | `createWebRouter(options)` | 创建 Web 模式路由器 |
455
+ | `createMemoryRouter(options)` | 创建 Memory 模式路由器 |
456
+ | `createRouteManager(routes, options?)` | 创建路由管理器 |
457
+ | `defineRoutes(...routes)` | 定义路由表 |
458
+ | `useRouter()` | 获取路由器实例 |
459
+ | `useRoute(global?)` | 获取当前路由信息 |
460
+ | `useLink(options)` | 创建链接助手 |
461
+ | `onBeforeRouteLeave(guard)` | 注册离开守卫 |
462
+ | `onBeforeRouteUpdate(callback)` | 注册更新钩子 |
592
463
 
593
464
  ### Router 实例方法
594
465
 
595
- | 方法 | 说明 |
596
- |--------------------|-----------|
597
- | `push(options)` | 跳转到新路由 |
598
- | `replace(options)` | 替换当前路由 |
599
- | `go(delta)` | 前进/后退指定步数 |
600
- | `back()` | 后退一步 |
601
- | `forward()` | 前进一步 |
602
- | `scrollTo(target)` | 滚动到指定位置 |
603
- | `initialize()` | 初始化路由器 |
466
+ | 方法 | 说明 |
467
+ |----------------------------|-----------|
468
+ | `push(target)` | 跳转到新路由 |
469
+ | `replace(target)` | 替换当前路由 |
470
+ | `go(delta)` | 前进/后退指定步数 |
471
+ | `back()` | 后退一步 |
472
+ | `forward()` | 前进一步 |
473
+ | `addRoute(route, parent?)` | 添加路由 |
474
+ | `removeRoute(index)` | 移除路由 |
475
+ | `hasRoute(index)` | 检查路由是否存在 |
476
+ | `matchRoute(target)` | 匹配路由 |
477
+ | `beforeEach(guard)` | 添加前置守卫 |
478
+ | `afterEach(callback)` | 添加后置回调 |
479
+ | `destroy()` | 销毁路由器 |
604
480
 
605
481
  ### Router 实例属性
606
482
 
607
- | 属性 | 说明 |
608
- |------------------|---------------|
609
- | `route` | 当前路由信息(只读响应式) |
610
- | `options` | 路由器配置选项 |
611
- | `mode` | 路由模式 |
612
- | `isBrowser` | 是否浏览器环境 |
613
- | `scrollBehavior` | 滚动行为 |
614
-
615
- ---
483
+ | 属性 | 类型 | 说明 |
484
+ |-----------|---------------------------------|-----------|
485
+ | `route` | `ReadonlyObject<RouteLocation>` | 当前路由信息 |
486
+ | `routes` | `RouteRecord[]` | 所有注册的路由记录 |
487
+ | `config` | `ResolvedRouterConfig` | 路由器配置 |
488
+ | `manager` | `RouteManager` | 路由管理器实例 |
616
489
 
617
490
  ## License
618
491