Skip to content

Commit

Permalink
fix: useRoute is not reactive #2
Browse files Browse the repository at this point in the history
  • Loading branch information
fanwei committed Nov 9, 2021
1 parent 4add7e0 commit 0651468
Showing 1 changed file with 23 additions and 27 deletions.
50 changes: 23 additions & 27 deletions src/vue-router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,36 +50,32 @@ export interface RouteLocationNormalized extends Route {}
export interface RouteLocationNormalizedLoaded extends Route {}


function createReactiveRoute(initialRoute: Route) {
const routeRef = shallowRef(initialRoute);
const computedRoute = {} as {
[key in keyof Route]: ComputedRef<Route[key]>
}
for (const key of [
'name', 'meta', 'path', 'hash', 'query',
'params', 'fullPath', 'matched', 'redirectedFrom'
] as const) {
computedRoute[key] = computed<any>(() => routeRef.value[key]);
}
return [
reactive(computedRoute),
(route: Route) => {
routeRef.value = route
},
] as const
}

let reactiveCurrentRoute: Route
let currentRoute: RouteLocationNormalizedLoaded

export function useRoute(): RouteLocationNormalizedLoaded {
export function useRoute() {
const router = useRouter()
if (!router) return undefined as any
if (!reactiveCurrentRoute) {
let setCurrentRoute: (route: Route) => void
[reactiveCurrentRoute, setCurrentRoute] = createReactiveRoute(router.currentRoute)
router.afterEach(to => setCurrentRoute(to))
if (!currentRoute) {
const routeRef = shallowRef({
path: '/',
name: undefined,
params: {},
query: {},
hash: '',
fullPath: '/',
matched: [],
meta: {},
redirectedFrom: undefined,
} as Route);
const computedRoute = {} as {
[key in keyof Route]: ComputedRef<Route[key]>
}
for (const key of Object.keys(routeRef.value) as (keyof Route)[]) {
computedRoute[key] = computed<any>(() => routeRef.value[key])
}
router.afterEach(to => routeRef.value = to)
currentRoute = reactive(computedRoute)
}
return reactiveCurrentRoute
return currentRoute
}


Expand Down

0 comments on commit 0651468

Please sign in to comment.