@@ -25,7 +25,12 @@ import {
25
25
scrollToPosition ,
26
26
_ScrollPositionNormalized ,
27
27
} from './scrollBehavior'
28
- import { createRouterMatcher , PathParserOptions } from './matcher'
28
+ import {
29
+ CloneableRouterMatcher ,
30
+ createRouterMatcher ,
31
+ GenericRouterMatcher ,
32
+ PathParserOptions ,
33
+ } from './matcher'
29
34
import {
30
35
createRouterError ,
31
36
ErrorTypes ,
@@ -97,10 +102,11 @@ export interface RouterScrollBehavior {
97
102
) : Awaitable < ScrollPosition | false | void >
98
103
}
99
104
100
- /**
101
- * Options to initialize a {@link Router} instance.
102
- */
103
- export interface RouterOptions extends PathParserOptions {
105
+ export interface DefaultMatcherOptions extends PathParserOptions {
106
+ routes : Readonly < RouteRecordRaw [ ] >
107
+ }
108
+
109
+ export interface SharedOptions {
104
110
/**
105
111
* History implementation used by the router. Most web applications should use
106
112
* `createWebHistory` but it requires the server to be properly configured.
@@ -117,10 +123,6 @@ export interface RouterOptions extends PathParserOptions {
117
123
* ```
118
124
*/
119
125
history : RouterHistory
120
- /**
121
- * Initial list of routes that should be added to the router.
122
- */
123
- routes : Readonly < RouteRecordRaw [ ] >
124
126
/**
125
127
* Function to control scrolling when navigating between pages. Can return a
126
128
* Promise to delay scrolling. Check {@link ScrollBehavior}.
@@ -174,10 +176,24 @@ export interface RouterOptions extends PathParserOptions {
174
176
// linkInactiveClass?: string
175
177
}
176
178
179
+ /**
180
+ * Options to initialize a {@link Router} instance.
181
+ */
182
+ export interface RouterOptions extends SharedOptions , PathParserOptions {
183
+ /**
184
+ * Initial list of routes that should be added to the router.
185
+ */
186
+ routes : Readonly < RouteRecordRaw [ ] >
187
+ }
188
+
189
+ export interface RouterWithMatcherOptions < RC > extends SharedOptions {
190
+ matcher : GenericRouterMatcher < RC >
191
+ }
192
+
177
193
/**
178
194
* Router instance.
179
195
*/
180
- export interface Router {
196
+ export interface GenericRouter < RC > {
181
197
/**
182
198
* @internal
183
199
*/
@@ -189,7 +205,7 @@ export interface Router {
189
205
/**
190
206
* Original options object passed to create the Router
191
207
*/
192
- readonly options : RouterOptions
208
+ readonly options : SharedOptions
193
209
194
210
/**
195
211
* Allows turning off the listening of history events. This is a low level api for micro-frontends.
@@ -202,13 +218,13 @@ export interface Router {
202
218
* @param parentName - Parent Route Record where `route` should be appended at
203
219
* @param route - Route Record to add
204
220
*/
205
- addRoute ( parentName : RouteRecordName , route : RouteRecordRaw ) : ( ) => void
221
+ addRoute ( parentName : RouteRecordName , route : RC ) : ( ) => void
206
222
/**
207
223
* Add a new {@link RouteRecordRaw | route record} to the router.
208
224
*
209
225
* @param route - Route Record to add
210
226
*/
211
- addRoute ( route : RouteRecordRaw ) : ( ) => void
227
+ addRoute ( route : RC ) : ( ) => void
212
228
/**
213
229
* Remove an existing route by its name.
214
230
*
@@ -260,12 +276,12 @@ export interface Router {
260
276
* Go back in history if possible by calling `history.back()`. Equivalent to
261
277
* `router.go(-1)`.
262
278
*/
263
- back ( ) : ReturnType < Router [ 'go' ] >
279
+ back ( ) : ReturnType < GenericRouter < RC > [ 'go' ] >
264
280
/**
265
281
* Go forward in history if possible by calling `history.forward()`.
266
282
* Equivalent to `router.go(1)`.
267
283
*/
268
- forward ( ) : ReturnType < Router [ 'go' ] >
284
+ forward ( ) : ReturnType < GenericRouter < RC > [ 'go' ] >
269
285
/**
270
286
* Allows you to move forward or backward through the history. Calls
271
287
* `history.go()`.
@@ -352,13 +368,44 @@ export interface Router {
352
368
install ( app : App ) : void
353
369
}
354
370
371
+ export interface Router extends GenericRouter < RouteRecordRaw > {
372
+ readonly options : RouterOptions
373
+ }
374
+
375
+ export interface RouterWithMatcher < RC > extends GenericRouter < RC > {
376
+ readonly options : RouterWithMatcherOptions < RC >
377
+ }
378
+
379
+ export function createDefaultMatcher (
380
+ options : DefaultMatcherOptions
381
+ ) : CloneableRouterMatcher {
382
+ return createRouterMatcher ( options . routes , options )
383
+ }
384
+
355
385
/**
356
386
* Creates a Router instance that can be used by a Vue app.
357
387
*
358
388
* @param options - {@link RouterOptions}
359
389
*/
360
390
export function createRouter ( options : RouterOptions ) : Router {
361
- const matcher = createRouterMatcher ( options . routes , options )
391
+ const matcher = createDefaultMatcher ( options )
392
+
393
+ const router = createRouterWithMatcher ( {
394
+ ...options ,
395
+ matcher,
396
+ } )
397
+
398
+ // Set the original options
399
+ ; ( router as any ) . options = options
400
+
401
+ // Casting needed due to the 'options' property
402
+ return router as GenericRouter < RouteRecordRaw > as Router
403
+ }
404
+
405
+ export function createRouterWithMatcher < RC > (
406
+ options : RouterWithMatcherOptions < RC >
407
+ ) : RouterWithMatcher < RC > {
408
+ const matcher = options . matcher
362
409
const parseQuery = options . parseQuery || originalParseQuery
363
410
const stringifyQuery = options . stringifyQuery || originalStringifyQuery
364
411
const routerHistory = options . history
@@ -390,12 +437,9 @@ export function createRouter(options: RouterOptions): Router {
390
437
// @ts -expect-error: intentionally avoid the type check
391
438
applyToParams . bind ( null , decode )
392
439
393
- function addRoute (
394
- parentOrRoute : RouteRecordName | RouteRecordRaw ,
395
- route ?: RouteRecordRaw
396
- ) {
440
+ function addRoute ( parentOrRoute : RouteRecordName | RC , route ?: RC ) {
397
441
let parent : Parameters < ( typeof matcher ) [ 'addRoute' ] > [ 1 ] | undefined
398
- let record : RouteRecordRaw
442
+ let record : RC
399
443
if ( isRouteName ( parentOrRoute ) ) {
400
444
parent = matcher . getRecordMatcher ( parentOrRoute )
401
445
if ( __DEV__ && ! parent ) {
@@ -1201,7 +1245,7 @@ export function createRouter(options: RouterOptions): Router {
1201
1245
let started : boolean | undefined
1202
1246
const installedApps = new Set < App > ( )
1203
1247
1204
- const router : Router = {
1248
+ const router : RouterWithMatcher < RC > = {
1205
1249
currentRoute,
1206
1250
listening : true ,
1207
1251
@@ -1230,7 +1274,7 @@ export function createRouter(options: RouterOptions): Router {
1230
1274
app . component ( 'RouterLink' , RouterLink )
1231
1275
app . component ( 'RouterView' , RouterView )
1232
1276
1233
- app . config . globalProperties . $router = router
1277
+ app . config . globalProperties . $router = router as any
1234
1278
Object . defineProperty ( app . config . globalProperties , '$route' , {
1235
1279
enumerable : true ,
1236
1280
get : ( ) => unref ( currentRoute ) ,
@@ -1261,7 +1305,7 @@ export function createRouter(options: RouterOptions): Router {
1261
1305
} )
1262
1306
}
1263
1307
1264
- app . provide ( routerKey , router )
1308
+ app . provide ( routerKey , router as any )
1265
1309
app . provide ( routeLocationKey , shallowReactive ( reactiveRoute ) )
1266
1310
app . provide ( routerViewLocationKey , currentRoute )
1267
1311
0 commit comments