@@ -50,36 +50,32 @@ export interface RouteLocationNormalized extends Route {}
50
50
export interface RouteLocationNormalizedLoaded extends Route { }
51
51
52
52
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
73
54
74
- export function useRoute ( ) : RouteLocationNormalizedLoaded {
55
+ export function useRoute ( ) {
75
56
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 )
81
77
}
82
- return reactiveCurrentRoute
78
+ return currentRoute
83
79
}
84
80
85
81
0 commit comments