From 7780592d61c2996b4d169ea98c640282db668194 Mon Sep 17 00:00:00 2001 From: Admin Date: Thu, 9 Feb 2023 10:54:01 +0800 Subject: [PATCH 1/2] perf(guard): Add a tool function to handle the logic of TargetRouterViewDepth --- src/composables/guards.js | 13 +++---------- src/util/route.js | 5 +++++ 2 files changed, 8 insertions(+), 10 deletions(-) diff --git a/src/composables/guards.js b/src/composables/guards.js index 6312e9401..30442d489 100644 --- a/src/composables/guards.js +++ b/src/composables/guards.js @@ -1,5 +1,5 @@ import { getCurrentInstance, onUnmounted } from 'vue' -import { throwNoCurrentInstance } from './utils' +import { throwNoCurrentInstance, getTargetRouterViewDepth } from './utils' import { useRouter } from './globals' export function onBeforeRouteUpdate (guard) { @@ -42,19 +42,12 @@ function useFilteredGuard (guard, fn) { let target = instance.proxy // find the nearest RouterView to know the depth while ( - target && - target.$vnode && - target.$vnode.data && - target.$vnode.data.routerViewDepth == null + getTargetRouterViewDepth(target) ) { target = target.$parent } - const depth = - target && target.$vnode && target.$vnode.data - ? target.$vnode.data.routerViewDepth - : null - + const depth = getTargetRouterViewDepth(target) if (depth != null) { const removeGuard = router.beforeEach((to, from, next) => { return fn(to, from, depth) ? guard(to, from, next) : next() diff --git a/src/util/route.js b/src/util/route.js index 9d8e31cd1..f0e1ff990 100644 --- a/src/util/route.js +++ b/src/util/route.js @@ -149,3 +149,8 @@ export function handleRouteEntered (route: Route) { } } } + +export function getTargetRouterViewDepth (target) { + return target && target.$vnode && target.$vnode.data ? target.$vnode.data.routerViewDepth + : null +} From 810e782e661bb8eb2e139686d5d9287c2371f165 Mon Sep 17 00:00:00 2001 From: Admin Date: Thu, 9 Feb 2023 12:00:49 +0800 Subject: [PATCH 2/2] perf(guard): Add a tool function to handle the logic of TargetRouterViewDepth --- src/util/route.js | 2 +- types/router.d.ts | 5 +++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/src/util/route.js b/src/util/route.js index f0e1ff990..1edfb2bfc 100644 --- a/src/util/route.js +++ b/src/util/route.js @@ -150,7 +150,7 @@ export function handleRouteEntered (route: Route) { } } -export function getTargetRouterViewDepth (target) { +export function getTargetRouterViewDepth (target: any) { return target && target.$vnode && target.$vnode.data ? target.$vnode.data.routerViewDepth : null } diff --git a/types/router.d.ts b/types/router.d.ts index a334bc95f..f0784c297 100644 --- a/types/router.d.ts +++ b/types/router.d.ts @@ -354,12 +354,13 @@ interface RouteConfigMultipleViews extends _RouteConfigBase { } export type RouteConfig = RouteConfigSingleView | RouteConfigMultipleViews +export type VueInstance = Dictionary export interface RouteRecord { path: string regex: RegExp components: Dictionary - instances: Dictionary + instances: VueInstance name?: string parent?: RouteRecord redirect?: RedirectOption @@ -380,7 +381,7 @@ export interface RouteRecord { export interface RouteRecordPublic { path: string components: Dictionary - instances: Dictionary + instances: VueInstance name?: string redirect?: RedirectOption meta: any