From 4e79e8384db88051bebb552fadb233519dee9f6d Mon Sep 17 00:00:00 2001 From: jingyu Date: Mon, 17 Jan 2022 18:22:49 +0800 Subject: [PATCH] fix(vue-router): fix useRoute is not reactive & useRoute initial value is default value fix #1 --- src/vue-router.ts | 26 +++++++------------------- 1 file changed, 7 insertions(+), 19 deletions(-) diff --git a/src/vue-router.ts b/src/vue-router.ts index 397029e..af4620b 100644 --- a/src/vue-router.ts +++ b/src/vue-router.ts @@ -1,5 +1,5 @@ import Vue from 'vue' -import { computed, ComputedRef, getCurrentInstance, reactive, shallowRef } from '@vue/composition-api' +import { getCurrentInstance, reactive } from '@vue/composition-api' import VueRouter, { NavigationGuard, Route, RouterOptions } from 'vue-router' import { OUT_OF_SCOPE, warn } from './utils' @@ -61,25 +61,13 @@ let currentRoute: RouteLocationNormalizedLoaded export function useRoute() { const router = useRouter() 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 + const inst = getCurrentInstance() + if (!inst) { + warn(OUT_OF_SCOPE) + return } - for (const key of Object.keys(routeRef.value) as (keyof Route)[]) { - computedRoute[key] = computed(() => routeRef.value[key]) - } - router.afterEach(to => routeRef.value = to) - currentRoute = reactive(computedRoute) + currentRoute = reactive({...inst.proxy.$route} as Route) + router.afterEach(to => Object.assign(currentRoute, to)) } return currentRoute }