@@ -25,7 +25,12 @@ import {
2525 scrollToPosition ,
2626 _ScrollPositionNormalized ,
2727} from './scrollBehavior'
28- import { createRouterMatcher , PathParserOptions } from './matcher'
28+ import {
29+ CloneableRouterMatcher ,
30+ createRouterMatcher ,
31+ GenericRouterMatcher ,
32+ PathParserOptions ,
33+ } from './matcher'
2934import {
3035 createRouterError ,
3136 ErrorTypes ,
@@ -97,10 +102,11 @@ export interface RouterScrollBehavior {
97102 ) : Awaitable < ScrollPosition | false | void >
98103}
99104
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 {
104110 /**
105111 * History implementation used by the router. Most web applications should use
106112 * `createWebHistory` but it requires the server to be properly configured.
@@ -117,10 +123,6 @@ export interface RouterOptions extends PathParserOptions {
117123 * ```
118124 */
119125 history : RouterHistory
120- /**
121- * Initial list of routes that should be added to the router.
122- */
123- routes : Readonly < RouteRecordRaw [ ] >
124126 /**
125127 * Function to control scrolling when navigating between pages. Can return a
126128 * Promise to delay scrolling. Check {@link ScrollBehavior}.
@@ -174,10 +176,24 @@ export interface RouterOptions extends PathParserOptions {
174176 // linkInactiveClass?: string
175177}
176178
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+
177193/**
178194 * Router instance.
179195 */
180- export interface Router {
196+ export interface GenericRouter < RC > {
181197 /**
182198 * @internal
183199 */
@@ -189,7 +205,7 @@ export interface Router {
189205 /**
190206 * Original options object passed to create the Router
191207 */
192- readonly options : RouterOptions
208+ readonly options : SharedOptions
193209
194210 /**
195211 * Allows turning off the listening of history events. This is a low level api for micro-frontends.
@@ -202,13 +218,13 @@ export interface Router {
202218 * @param parentName - Parent Route Record where `route` should be appended at
203219 * @param route - Route Record to add
204220 */
205- addRoute ( parentName : RouteRecordName , route : RouteRecordRaw ) : ( ) => void
221+ addRoute ( parentName : RouteRecordName , route : RC ) : ( ) => void
206222 /**
207223 * Add a new {@link RouteRecordRaw | route record} to the router.
208224 *
209225 * @param route - Route Record to add
210226 */
211- addRoute ( route : RouteRecordRaw ) : ( ) => void
227+ addRoute ( route : RC ) : ( ) => void
212228 /**
213229 * Remove an existing route by its name.
214230 *
@@ -260,12 +276,12 @@ export interface Router {
260276 * Go back in history if possible by calling `history.back()`. Equivalent to
261277 * `router.go(-1)`.
262278 */
263- back ( ) : ReturnType < Router [ 'go' ] >
279+ back ( ) : ReturnType < GenericRouter < RC > [ 'go' ] >
264280 /**
265281 * Go forward in history if possible by calling `history.forward()`.
266282 * Equivalent to `router.go(1)`.
267283 */
268- forward ( ) : ReturnType < Router [ 'go' ] >
284+ forward ( ) : ReturnType < GenericRouter < RC > [ 'go' ] >
269285 /**
270286 * Allows you to move forward or backward through the history. Calls
271287 * `history.go()`.
@@ -352,13 +368,44 @@ export interface Router {
352368 install ( app : App ) : void
353369}
354370
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+
355385/**
356386 * Creates a Router instance that can be used by a Vue app.
357387 *
358388 * @param options - {@link RouterOptions}
359389 */
360390export 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
362409 const parseQuery = options . parseQuery || originalParseQuery
363410 const stringifyQuery = options . stringifyQuery || originalStringifyQuery
364411 const routerHistory = options . history
@@ -390,12 +437,9 @@ export function createRouter(options: RouterOptions): Router {
390437 // @ts -expect-error: intentionally avoid the type check
391438 applyToParams . bind ( null , decode )
392439
393- function addRoute (
394- parentOrRoute : RouteRecordName | RouteRecordRaw ,
395- route ?: RouteRecordRaw
396- ) {
440+ function addRoute ( parentOrRoute : RouteRecordName | RC , route ?: RC ) {
397441 let parent : Parameters < ( typeof matcher ) [ 'addRoute' ] > [ 1 ] | undefined
398- let record : RouteRecordRaw
442+ let record : RC
399443 if ( isRouteName ( parentOrRoute ) ) {
400444 parent = matcher . getRecordMatcher ( parentOrRoute )
401445 if ( __DEV__ && ! parent ) {
@@ -1201,7 +1245,7 @@ export function createRouter(options: RouterOptions): Router {
12011245 let started : boolean | undefined
12021246 const installedApps = new Set < App > ( )
12031247
1204- const router : Router = {
1248+ const router : RouterWithMatcher < RC > = {
12051249 currentRoute,
12061250 listening : true ,
12071251
@@ -1230,7 +1274,7 @@ export function createRouter(options: RouterOptions): Router {
12301274 app . component ( 'RouterLink' , RouterLink )
12311275 app . component ( 'RouterView' , RouterView )
12321276
1233- app . config . globalProperties . $router = router
1277+ app . config . globalProperties . $router = router as any
12341278 Object . defineProperty ( app . config . globalProperties , '$route' , {
12351279 enumerable : true ,
12361280 get : ( ) => unref ( currentRoute ) ,
@@ -1261,7 +1305,7 @@ export function createRouter(options: RouterOptions): Router {
12611305 } )
12621306 }
12631307
1264- app . provide ( routerKey , router )
1308+ app . provide ( routerKey , router as any )
12651309 app . provide ( routeLocationKey , shallowReactive ( reactiveRoute ) )
12661310 app . provide ( routerViewLocationKey , currentRoute )
12671311
0 commit comments