@@ -48,27 +48,36 @@ export interface RouteLocationNormalized extends Route {}
4848export interface RouteLocationNormalizedLoaded extends Route { }
4949
5050
51- const currentRoute = shallowRef ( VueRouter . START_LOCATION ) ;
52- const computedRoute = { } as {
53- [ key in keyof Route ] : ComputedRef < Route [ key ] >
54- }
55- for ( const key of [
56- 'name' , 'meta' , 'path' , 'hash' , 'query' ,
57- 'params' , 'fullPath' , 'matched' , 'redirectedFrom'
58- ] as const ) {
59- computedRoute [ key ] = computed < any > ( ( ) => currentRoute . value [ key ] ) ;
51+ function createReactiveRoute ( initialRoute : Route ) {
52+ const routeRef = shallowRef ( initialRoute ) ;
53+ const computedRoute = { } as {
54+ [ key in keyof Route ] : ComputedRef < Route [ key ] >
55+ }
56+ for ( const key of [
57+ 'name' , 'meta' , 'path' , 'hash' , 'query' ,
58+ 'params' , 'fullPath' , 'matched' , 'redirectedFrom'
59+ ] as const ) {
60+ computedRoute [ key ] = computed < any > ( ( ) => routeRef . value [ key ] ) ;
61+ }
62+ return [
63+ reactive ( computedRoute ) ,
64+ ( route : Route ) => {
65+ routeRef . value = route
66+ } ,
67+ ] as const
6068}
61- let reactiveRoute : Route
69+
70+ let reactiveCurrentRoute : Route
6271
6372export function useRoute ( ) : RouteLocationNormalizedLoaded {
6473 const router = useRouter ( )
6574 if ( ! router ) return undefined as any
66- if ( ! reactiveRoute ) {
67- currentRoute . value = router . currentRoute
68- router . afterEach ( to => currentRoute . value = to ) ;
69- reactiveRoute = reactive ( computedRoute )
75+ if ( ! reactiveCurrentRoute ) {
76+ let setCurrentRoute : ( route : Route ) => void
77+ [ reactiveCurrentRoute , setCurrentRoute ] = createReactiveRoute ( router . currentRoute )
78+ router . afterEach ( to => setCurrentRoute ( to ) )
7079 }
71- return reactiveRoute
80+ return reactiveCurrentRoute
7281}
7382
7483
0 commit comments