Skip to content

Commit

Permalink
fix: call shallowRef after installing C... API
Browse files Browse the repository at this point in the history
  • Loading branch information
fanwei committed Sep 13, 2021
1 parent 6b017c3 commit afef4b9
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 16 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "vue2-helpers",
"version": "1.1.4",
"version": "1.1.5",
"description": "A util package to use Vue 2 with Composition API easily",
"author": "Ambit Tsai <ambit_tsai@qq.com>",
"license": "Apache-2.0",
Expand Down
39 changes: 24 additions & 15 deletions src/vue-router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,27 +48,36 @@ export interface RouteLocationNormalized extends Route {}
export interface RouteLocationNormalizedLoaded extends Route {}


const currentRoute = shallowRef(VueRouter.START_LOCATION);
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>(() => currentRoute.value[key]);
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 reactiveRoute: Route

let reactiveCurrentRoute: Route

export function useRoute(): RouteLocationNormalizedLoaded {
const router = useRouter()
if (!router) return undefined as any
if (!reactiveRoute) {
currentRoute.value = router.currentRoute
router.afterEach(to => currentRoute.value = to);
reactiveRoute = reactive(computedRoute)
if (!reactiveCurrentRoute) {
let setCurrentRoute: (route: Route) => void
[reactiveCurrentRoute, setCurrentRoute] = createReactiveRoute(router.currentRoute)
router.afterEach(to => setCurrentRoute(to))
}
return reactiveRoute
return reactiveCurrentRoute
}


Expand Down

0 comments on commit afef4b9

Please sign in to comment.