Skip to content

Commit 7780592

Browse files
AdminAdmin
Admin
authored and
Admin
committed
perf(guard): Add a tool function to handle the logic of TargetRouterViewDepth
1 parent e6ff070 commit 7780592

File tree

2 files changed

+8
-10
lines changed

2 files changed

+8
-10
lines changed

Diff for: src/composables/guards.js

+3-10
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { getCurrentInstance, onUnmounted } from 'vue'
2-
import { throwNoCurrentInstance } from './utils'
2+
import { throwNoCurrentInstance, getTargetRouterViewDepth } from './utils'
33
import { useRouter } from './globals'
44

55
export function onBeforeRouteUpdate (guard) {
@@ -42,19 +42,12 @@ function useFilteredGuard (guard, fn) {
4242
let target = instance.proxy
4343
// find the nearest RouterView to know the depth
4444
while (
45-
target &&
46-
target.$vnode &&
47-
target.$vnode.data &&
48-
target.$vnode.data.routerViewDepth == null
45+
getTargetRouterViewDepth(target)
4946
) {
5047
target = target.$parent
5148
}
5249

53-
const depth =
54-
target && target.$vnode && target.$vnode.data
55-
? target.$vnode.data.routerViewDepth
56-
: null
57-
50+
const depth = getTargetRouterViewDepth(target)
5851
if (depth != null) {
5952
const removeGuard = router.beforeEach((to, from, next) => {
6053
return fn(to, from, depth) ? guard(to, from, next) : next()

Diff for: src/util/route.js

+5
Original file line numberDiff line numberDiff line change
@@ -149,3 +149,8 @@ export function handleRouteEntered (route: Route) {
149149
}
150150
}
151151
}
152+
153+
export function getTargetRouterViewDepth (target) {
154+
return target && target.$vnode && target.$vnode.data ? target.$vnode.data.routerViewDepth
155+
: null
156+
}

0 commit comments

Comments
 (0)