Skip to content

Commit fb6635d

Browse files
authored
Merge pull request #6 from chen-junyi/main
fix(vue-router): fix useRoute is not reactive & useRoute initial valu…
2 parents 947e54c + 4e79e83 commit fb6635d

File tree

1 file changed

+7
-19
lines changed

1 file changed

+7
-19
lines changed

src/vue-router.ts

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import Vue from 'vue'
2-
import { computed, ComputedRef, getCurrentInstance, reactive, shallowRef } from '@vue/composition-api'
2+
import { getCurrentInstance, reactive } from '@vue/composition-api'
33
import VueRouter, { NavigationGuard, Route, RouterOptions } from 'vue-router'
44
import { OUT_OF_SCOPE, warn } from './utils'
55

@@ -61,25 +61,13 @@ let currentRoute: RouteLocationNormalizedLoaded
6161
export function useRoute() {
6262
const router = useRouter()
6363
if (!currentRoute) {
64-
const routeRef = shallowRef({
65-
path: '/',
66-
name: undefined,
67-
params: {},
68-
query: {},
69-
hash: '',
70-
fullPath: '/',
71-
matched: [],
72-
meta: {},
73-
redirectedFrom: undefined,
74-
} as Route);
75-
const computedRoute = {} as {
76-
[key in keyof Route]: ComputedRef<Route[key]>
64+
const inst = getCurrentInstance()
65+
if (!inst) {
66+
warn(OUT_OF_SCOPE)
67+
return
7768
}
78-
for (const key of Object.keys(routeRef.value) as (keyof Route)[]) {
79-
computedRoute[key] = computed<any>(() => routeRef.value[key])
80-
}
81-
router.afterEach(to => routeRef.value = to)
82-
currentRoute = reactive(computedRoute)
69+
currentRoute = reactive({...inst.proxy.$route} as Route)
70+
router.afterEach(to => Object.assign(currentRoute, to))
8371
}
8472
return currentRoute
8573
}

0 commit comments

Comments
 (0)