@@ -50,36 +50,32 @@ export interface RouteLocationNormalized extends Route {}
5050export interface RouteLocationNormalizedLoaded extends Route { }
5151
5252
53- function createReactiveRoute ( initialRoute : Route ) {
54- const routeRef = shallowRef ( initialRoute ) ;
55- const computedRoute = { } as {
56- [ key in keyof Route ] : ComputedRef < Route [ key ] >
57- }
58- for ( const key of [
59- 'name' , 'meta' , 'path' , 'hash' , 'query' ,
60- 'params' , 'fullPath' , 'matched' , 'redirectedFrom'
61- ] as const ) {
62- computedRoute [ key ] = computed < any > ( ( ) => routeRef . value [ key ] ) ;
63- }
64- return [
65- reactive ( computedRoute ) ,
66- ( route : Route ) => {
67- routeRef . value = route
68- } ,
69- ] as const
70- }
71-
72- let reactiveCurrentRoute : Route
53+ let currentRoute : RouteLocationNormalizedLoaded
7354
74- export function useRoute ( ) : RouteLocationNormalizedLoaded {
55+ export function useRoute ( ) {
7556 const router = useRouter ( )
76- if ( ! router ) return undefined as any
77- if ( ! reactiveCurrentRoute ) {
78- let setCurrentRoute : ( route : Route ) => void
79- [ reactiveCurrentRoute , setCurrentRoute ] = createReactiveRoute ( router . currentRoute )
80- router . afterEach ( to => setCurrentRoute ( to ) )
57+ if ( ! currentRoute ) {
58+ const routeRef = shallowRef ( {
59+ path : '/' ,
60+ name : undefined ,
61+ params : { } ,
62+ query : { } ,
63+ hash : '' ,
64+ fullPath : '/' ,
65+ matched : [ ] ,
66+ meta : { } ,
67+ redirectedFrom : undefined ,
68+ } as Route ) ;
69+ const computedRoute = { } as {
70+ [ key in keyof Route ] : ComputedRef < Route [ key ] >
71+ }
72+ for ( const key of Object . keys ( routeRef . value ) as ( keyof Route ) [ ] ) {
73+ computedRoute [ key ] = computed < any > ( ( ) => routeRef . value [ key ] )
74+ }
75+ router . afterEach ( to => routeRef . value = to )
76+ currentRoute = reactive ( computedRoute )
8177 }
82- return reactiveCurrentRoute
78+ return currentRoute
8379}
8480
8581
0 commit comments