@@ -16,20 +16,13 @@ import {
16
16
type App ,
17
17
} from 'vue'
18
18
import { RouterLink } from '../RouterLink'
19
- import { RouterView } from '../RouterView'
20
19
import {
21
20
NavigationType ,
22
21
type HistoryState ,
23
22
type RouterHistory ,
24
23
} from '../history/common'
25
24
import type { PathParserOptions } from '../matcher'
26
- import {
27
- type NEW_MatcherRecordBase ,
28
- type NEW_LocationResolved ,
29
- type NEW_MatcherRecord ,
30
- type NEW_MatcherRecordRaw ,
31
- type NEW_RouterResolver ,
32
- } from '../new-route-resolver/resolver'
25
+ import { type NEW_LocationResolved } from '../new-route-resolver/resolver'
33
26
import {
34
27
parseQuery as originalParseQuery ,
35
28
stringifyQuery as originalStringifyQuery ,
@@ -45,6 +38,7 @@ import {
45
38
type RouterScrollBehavior ,
46
39
} from '../scrollBehavior'
47
40
import type {
41
+ _RouteRecordProps ,
48
42
NavigationGuardWithThis ,
49
43
NavigationHookAfter ,
50
44
RouteLocation ,
@@ -61,8 +55,8 @@ import type {
61
55
} from '../typed-routes'
62
56
import {
63
57
isRouteLocation ,
64
- isRouteName ,
65
58
Lazy ,
59
+ RawRouteComponent ,
66
60
RouteLocationOptions ,
67
61
RouteMeta ,
68
62
} from '../types'
@@ -84,6 +78,10 @@ import {
84
78
routerKey ,
85
79
routerViewLocationKey ,
86
80
} from '../injectionSymbols'
81
+ import {
82
+ EXPERIMENTAL_ResolverStatic ,
83
+ EXPERIMENTAL_ResolverStaticRecord ,
84
+ } from '../new-route-resolver/resolver-static'
87
85
88
86
/**
89
87
* resolve, reject arguments of Promise constructor
@@ -179,30 +177,58 @@ export interface EXPERIMENTAL_RouterOptions_Base extends PathParserOptions {
179
177
// linkInactiveClass?: string
180
178
}
181
179
180
+ // TODO: is it worth to have 2 types for the undefined values?
181
+ export interface EXPERIMENTAL_RouteRecordNormalized
182
+ extends EXPERIMENTAL_ResolverStaticRecord {
183
+ /**
184
+ * Arbitrary data attached to the record.
185
+ */
186
+ meta : RouteMeta
187
+
188
+ // TODO:
189
+ redirect ?: unknown
190
+
191
+ /**
192
+ * Allow passing down params as props to the component rendered by `router-view`.
193
+ */
194
+ props : Record < string , _RouteRecordProps >
195
+
196
+ /**
197
+ * {@inheritDoc RouteRecordMultipleViews.components }
198
+ */
199
+ components : Record < string , RawRouteComponent >
200
+
201
+ /**
202
+ * Contains the original modules for lazy loaded components.
203
+ * @internal
204
+ */
205
+ mods : Record < string , unknown >
206
+ }
207
+
182
208
/**
183
209
* Options to initialize an experimental {@link EXPERIMENTAL_Router} instance.
184
210
* @experimental
185
211
*/
186
212
export interface EXPERIMENTAL_RouterOptions <
187
- TMatcherRecord extends NEW_MatcherRecord
188
- > extends EXPERIMENTAL_RouterOptions_Base {
189
- /**
190
- * Initial list of routes that should be added to the router.
191
- */
192
- routes ?: Readonly < EXPERIMENTAL_RouteRecordRaw [ ] >
193
-
213
+ // TODO: probably need some generic types
214
+ // TResolver extends NEW_RouterResolver_Base,
215
+ > extends EXPERIMENTAL_RouterOptions_Base {
194
216
/**
195
217
* Matcher to use to resolve routes.
218
+ *
196
219
* @experimental
197
220
*/
198
- resolver : NEW_RouterResolver < NEW_MatcherRecordRaw , TMatcherRecord >
221
+ resolver : EXPERIMENTAL_ResolverStatic < EXPERIMENTAL_RouteRecordNormalized >
199
222
}
200
223
201
224
/**
202
225
* Router base instance.
226
+ *
203
227
* @experimental This version is not stable, it's meant to replace {@link Router} in the future.
204
228
*/
205
- export interface EXPERIMENTAL_Router_Base < TRouteRecordRaw , TRouteRecord > {
229
+ export interface EXPERIMENTAL_Router_Base < TRecord > {
230
+ // NOTE: for dynamic routing we need this
231
+ // <TRouteRecordRaw, TRouteRecord>
206
232
/**
207
233
* Current {@link RouteLocationNormalized}
208
234
*/
@@ -213,31 +239,6 @@ export interface EXPERIMENTAL_Router_Base<TRouteRecordRaw, TRouteRecord> {
213
239
*/
214
240
listening : boolean
215
241
216
- /**
217
- * Add a new {@link EXPERIMENTAL_RouteRecordRaw | route record} as the child of an existing route.
218
- *
219
- * @param parentName - Parent Route Record where `route` should be appended at
220
- * @param route - Route Record to add
221
- */
222
- addRoute (
223
- // NOTE: it could be `keyof RouteMap` but the point of dynamic routes is not knowing the routes at build
224
- parentName : NonNullable < RouteRecordNameGeneric > ,
225
- route : TRouteRecordRaw
226
- ) : ( ) => void
227
- /**
228
- * Add a new {@link EXPERIMENTAL_RouteRecordRaw | route record} to the router.
229
- *
230
- * @param route - Route Record to add
231
- */
232
- addRoute ( route : TRouteRecordRaw ) : ( ) => void
233
-
234
- /**
235
- * Remove an existing route by its name.
236
- *
237
- * @param name - Name of the route to remove
238
- */
239
- removeRoute ( name : NonNullable < RouteRecordNameGeneric > ) : void
240
-
241
242
/**
242
243
* Checks if a route with a given name exists
243
244
*
@@ -248,12 +249,7 @@ export interface EXPERIMENTAL_Router_Base<TRouteRecordRaw, TRouteRecord> {
248
249
/**
249
250
* Get a full list of all the {@link RouteRecord | route records}.
250
251
*/
251
- getRoutes ( ) : TRouteRecord [ ]
252
-
253
- /**
254
- * Delete all routes from the router matcher.
255
- */
256
- clearRoutes ( ) : void
252
+ getRoutes ( ) : TRecord [ ]
257
253
258
254
/**
259
255
* Returns the {@link RouteLocation | normalized version} of a
@@ -392,65 +388,57 @@ export interface EXPERIMENTAL_Router_Base<TRouteRecordRaw, TRouteRecord> {
392
388
install ( app : App ) : void
393
389
}
394
390
395
- export interface EXPERIMENTAL_Router <
396
- TRouteRecordRaw , // extends NEW_MatcherRecordRaw,
397
- TRouteRecord extends NEW_MatcherRecord
398
- > extends EXPERIMENTAL_Router_Base < TRouteRecordRaw , TRouteRecord > {
391
+ export interface EXPERIMENTAL_Router
392
+ // TODO: dynamic routing
393
+ // <
394
+ // TRouteRecordRaw, // extends NEW_MatcherRecordRaw,
395
+ // TRouteRecord extends NEW_MatcherRecord,
396
+ // >
397
+ extends EXPERIMENTAL_Router_Base < EXPERIMENTAL_RouteRecordNormalized > {
399
398
/**
400
399
* Original options object passed to create the Router
401
400
*/
402
- readonly options : EXPERIMENTAL_RouterOptions < TRouteRecord >
403
- }
404
-
405
- export interface EXPERIMENTAL_RouteRecordRaw extends NEW_MatcherRecordRaw {
406
- /**
407
- * Arbitrary data attached to the record.
408
- */
409
- meta ?: RouteMeta
410
-
411
- components ?: Record < string , unknown >
412
- component ?: unknown
413
-
414
- redirect ?: unknown
415
- score : Array < number [ ] >
401
+ readonly options : EXPERIMENTAL_RouterOptions
416
402
}
417
403
418
- // TODO: is it worth to have 2 types for the undefined values?
419
- export interface EXPERIMENTAL_RouteRecordNormalized
420
- extends NEW_MatcherRecordBase < EXPERIMENTAL_RouteRecordNormalized > {
421
- /**
422
- * Arbitrary data attached to the record.
423
- */
424
- meta : RouteMeta
425
- group ?: boolean
426
- score : Array < number [ ] >
427
- }
428
-
429
- function normalizeRouteRecord (
430
- record : EXPERIMENTAL_RouteRecordRaw
431
- ) : EXPERIMENTAL_RouteRecordNormalized {
432
- // FIXME: implementation
433
- return {
434
- name : __DEV__ ? Symbol ( 'anonymous route record' ) : Symbol ( ) ,
435
- meta : { } ,
436
- ...record ,
437
- children : ( record . children || [ ] ) . map ( normalizeRouteRecord ) ,
438
- }
439
- }
404
+ // export interface EXPERIMENTAL_RouteRecordRaw extends NEW_MatcherRecordRaw {
405
+ // /**
406
+ // * Arbitrary data attached to the record.
407
+ // */
408
+ // meta?: RouteMeta
409
+ //
410
+ // components?: Record<string, unknown>
411
+ // component?: unknown
412
+ //
413
+ // redirect?: unknown
414
+ // // TODO: Not needed
415
+ // score: Array<number[]>
416
+ // }
417
+ //
418
+ //
419
+ // function normalizeRouteRecord(
420
+ // record: EXPERIMENTAL_RouteRecordRaw
421
+ // ): EXPERIMENTAL_RouteRecordNormalized {
422
+ // // FIXME: implementation
423
+ // return {
424
+ // name: __DEV__ ? Symbol('anonymous route record') : Symbol(),
425
+ // meta: {},
426
+ // ...record,
427
+ // children: (record.children || []).map(normalizeRouteRecord),
428
+ // }
429
+ // }
440
430
441
431
export function experimental_createRouter (
442
- options : EXPERIMENTAL_RouterOptions < EXPERIMENTAL_RouteRecordNormalized >
443
- ) : EXPERIMENTAL_Router <
444
- EXPERIMENTAL_RouteRecordRaw ,
445
- EXPERIMENTAL_RouteRecordNormalized
446
- > {
432
+ options : EXPERIMENTAL_RouterOptions
433
+ ) : EXPERIMENTAL_Router {
447
434
const {
448
435
resolver,
449
436
parseQuery = originalParseQuery ,
450
437
stringifyQuery = originalStringifyQuery ,
451
438
history : routerHistory ,
452
439
} = options
453
440
441
+ // FIXME: can be removed, it was for migration purposes
454
442
if ( __DEV__ && ! routerHistory )
455
443
throw new Error (
456
444
'Provide the "history" option when calling "createRouter()":' +
@@ -466,59 +454,16 @@ export function experimental_createRouter(
466
454
let pendingLocation : RouteLocation = START_LOCATION_NORMALIZED
467
455
468
456
// leave the scrollRestoration if no scrollBehavior is provided
469
- if ( isBrowser && options . scrollBehavior && 'scrollRestoration' in history ) {
457
+ if ( isBrowser && options . scrollBehavior ) {
470
458
history . scrollRestoration = 'manual'
471
459
}
472
460
473
- function addRoute (
474
- parentOrRoute :
475
- | NonNullable < RouteRecordNameGeneric >
476
- | EXPERIMENTAL_RouteRecordRaw ,
477
- route ?: EXPERIMENTAL_RouteRecordRaw
478
- ) {
479
- let parent : Parameters < ( typeof resolver ) [ 'addMatcher' ] > [ 1 ] | undefined
480
- let rawRecord : EXPERIMENTAL_RouteRecordRaw
481
-
482
- if ( isRouteName ( parentOrRoute ) ) {
483
- parent = resolver . getMatcher ( parentOrRoute )
484
- if ( __DEV__ && ! parent ) {
485
- warn (
486
- `Parent route "${ String (
487
- parentOrRoute
488
- ) } " not found when adding child route`,
489
- route
490
- )
491
- }
492
- rawRecord = route !
493
- } else {
494
- rawRecord = parentOrRoute
495
- }
496
-
497
- const addedRecord = resolver . addMatcher (
498
- normalizeRouteRecord ( rawRecord ) ,
499
- parent
500
- )
501
-
502
- return ( ) => {
503
- resolver . removeMatcher ( addedRecord )
504
- }
505
- }
506
-
507
- function removeRoute ( name : NonNullable < RouteRecordNameGeneric > ) {
508
- const recordMatcher = resolver . getMatcher ( name )
509
- if ( recordMatcher ) {
510
- resolver . removeMatcher ( recordMatcher )
511
- } else if ( __DEV__ ) {
512
- warn ( `Cannot remove non-existent route "${ String ( name ) } "` )
513
- }
514
- }
515
-
516
461
function getRoutes ( ) {
517
- return resolver . getMatchers ( )
462
+ return resolver . getRecords ( )
518
463
}
519
464
520
465
function hasRoute ( name : NonNullable < RouteRecordNameGeneric > ) : boolean {
521
- return ! ! resolver . getMatcher ( name )
466
+ return ! ! resolver . getRecord ( name )
522
467
}
523
468
524
469
function locationAsObject (
@@ -812,9 +757,10 @@ export function experimental_createRouter(
812
757
813
758
function runWithContext < T > ( fn : ( ) => T ) : T {
814
759
const app : App | undefined = installedApps . values ( ) . next ( ) . value
760
+ // FIXME: remove safeguard and ensure
815
761
// TODO: remove safeguard and bump required minimum version of Vue
816
762
// support Vue < 3.3
817
- return app && typeof app . runWithContext === 'function'
763
+ return typeof app ? .runWithContext === 'function'
818
764
? app . runWithContext ( fn )
819
765
: fn ( )
820
766
}
@@ -1223,16 +1169,10 @@ export function experimental_createRouter(
1223
1169
let started : boolean | undefined
1224
1170
const installedApps = new Set < App > ( )
1225
1171
1226
- const router : EXPERIMENTAL_Router <
1227
- EXPERIMENTAL_RouteRecordRaw ,
1228
- EXPERIMENTAL_RouteRecordNormalized
1229
- > = {
1172
+ const router : EXPERIMENTAL_Router = {
1230
1173
currentRoute,
1231
1174
listening : true ,
1232
1175
1233
- addRoute,
1234
- removeRoute,
1235
- clearRoutes : resolver . clearMatchers ,
1236
1176
hasRoute,
1237
1177
getRoutes,
1238
1178
resolve,
@@ -1252,9 +1192,9 @@ export function experimental_createRouter(
1252
1192
isReady,
1253
1193
1254
1194
install ( app : App ) {
1255
- const router = this
1256
- app . component ( 'RouterLink' , RouterLink )
1257
- app . component ( 'RouterView' , RouterView )
1195
+ // Must be done by user for vapor variants
1196
+ // app.component('RouterLink', RouterLink)
1197
+ // app.component('RouterView', RouterView)
1258
1198
1259
1199
// @ts -expect-error: FIXME: refactor with new types once it's possible
1260
1200
app . config . globalProperties . $router = router
@@ -1293,9 +1233,8 @@ export function experimental_createRouter(
1293
1233
app . provide ( routeLocationKey , shallowReactive ( reactiveRoute ) )
1294
1234
app . provide ( routerViewLocationKey , currentRoute )
1295
1235
1296
- const unmountApp = app . unmount
1297
1236
installedApps . add ( app )
1298
- app . unmount = function ( ) {
1237
+ app . onUnmount ( ( ) => {
1299
1238
installedApps . delete ( app )
1300
1239
// the router is not attached to an app anymore
1301
1240
if ( installedApps . size < 1 ) {
@@ -1307,8 +1246,7 @@ export function experimental_createRouter(
1307
1246
started = false
1308
1247
ready = false
1309
1248
}
1310
- unmountApp ( )
1311
- }
1249
+ } )
1312
1250
1313
1251
// TODO: this probably needs to be updated so it can be used by vue-termui
1314
1252
if ( ( __DEV__ || __FEATURE_PROD_DEVTOOLS__ ) && isBrowser ) {
0 commit comments