From: Eduardo San Martin Morote Date: Thu, 26 Mar 2020 16:45:12 +0000 (+0100) Subject: feat: improve route access X-Git-Tag: v4.0.0-alpha.4~13 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=baf266cd1bd6cafd32d244f185e340bee10af32c;p=thirdparty%2Fvuejs%2Frouter.git feat: improve route access BREAKING CHANGE: `useRoute` now retrieves a reactive RouteLocationNormalized instead of a Ref. This means there is no need to use `.value` when accessing the route. You still need to wrap it with `toRefs` if you want to expose parts of the route: ```js setup () { return { params: toRefs(useRoute()).params } } ``` --- diff --git a/playground/App.vue b/playground/App.vue index a18060e6..bd763a02 100644 --- a/playground/App.vue +++ b/playground/App.vue @@ -158,7 +158,7 @@ export default defineComponent({ const state = inject('state') const currentLocation = computed(() => { - const { matched, ...rest } = route.value + const { matched, ...rest } = route return rest }) diff --git a/src/index.ts b/src/index.ts index cbb6054a..7eaab8c6 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,9 +1,8 @@ import createWebHistory from './history/html5' import createMemoryHistory from './history/memory' import createWebHashHistory from './history/hash' -import { inject, computed, reactive } from 'vue' +import { inject } from 'vue' import { routerKey, routeLocationKey } from './utils/injectionSymbols' -import { RouteLocationNormalizedResolved } from './types' export { LocationQuery, parseQuery, stringifyQuery } from './utils/query' @@ -34,11 +33,5 @@ export function useRouter() { } export function useRoute() { - const route = inject(routeLocationKey)! - const ret = {} as RouteLocationNormalizedResolved - for (let key in route.value) { - // @ts-ignore - ret[key] = computed(() => route.value[key]) - } - return reactive(ret) + return inject(routeLocationKey)! } diff --git a/src/router.ts b/src/router.ts index 667398bf..f3412f23 100644 --- a/src/router.ts +++ b/src/router.ts @@ -38,7 +38,17 @@ import { parseQuery as originalParseQuery, stringifyQuery as originalStringifyQuery, } from './utils/query' -import { ref, Ref, markNonReactive, nextTick, App, warn } from 'vue' +import { + ref, + Ref, + markNonReactive, + nextTick, + App, + warn, + computed, + reactive, + ComputedRef, +} from 'vue' import { RouteRecordNormalized } from './matcher/types' import { Link } from './components/Link' import { View } from './components/View' @@ -564,9 +574,19 @@ function applyRouterPlugin(app: App, router: Router) { }, }) + const reactiveRoute = {} as { + [k in keyof RouteLocationNormalizedResolved]: ComputedRef< + RouteLocationNormalizedResolved[k] + > + } + for (let key in START_LOCATION_NORMALIZED) { + // @ts-ignore: the key matches + reactiveRoute[key] = computed(() => router.currentRoute.value[key]) + } + // TODO: merge strats? app.provide(routerKey, router) - app.provide(routeLocationKey, router.currentRoute) + app.provide(routeLocationKey, reactive(reactiveRoute)) } async function runGuardQueue(guards: Lazy[]): Promise { diff --git a/src/utils/injectionSymbols.ts b/src/utils/injectionSymbols.ts index 7f4f6e8c..5921c0ad 100644 --- a/src/utils/injectionSymbols.ts +++ b/src/utils/injectionSymbols.ts @@ -1,4 +1,4 @@ -import { InjectionKey, ComputedRef, Ref } from 'vue' +import { InjectionKey, ComputedRef } from 'vue' import { RouteLocationMatched, Immutable, @@ -24,5 +24,5 @@ export const viewDepthKey = PolySymbol('rvd') as InjectionKey export const routerKey = PolySymbol('r') as InjectionKey // rt = route location export const routeLocationKey = PolySymbol('rl') as InjectionKey< - Ref> + Immutable >