Skip to content

Commit afef4b9

Browse files
author
fanwei
committed
fix: call shallowRef after installing C... API
1 parent 6b017c3 commit afef4b9

File tree

2 files changed

+25
-16
lines changed

2 files changed

+25
-16
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "vue2-helpers",
3-
"version": "1.1.4",
3+
"version": "1.1.5",
44
"description": "A util package to use Vue 2 with Composition API easily",
55
"author": "Ambit Tsai <ambit_tsai@qq.com>",
66
"license": "Apache-2.0",

src/vue-router.ts

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -48,27 +48,36 @@ export interface RouteLocationNormalized extends Route {}
4848
export 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

6372
export 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

Comments
 (0)