Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf(guard): Add a tool function to handle the logic of TargetRouterV… #3831

Open
wants to merge 2 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 3 additions & 10 deletions src/composables/guards.js
Original file line number Diff line number Diff line change
@@ -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) {
Expand Down Expand Up @@ -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()
Expand Down
5 changes: 5 additions & 0 deletions src/util/route.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,3 +149,8 @@ export function handleRouteEntered (route: Route) {
}
}
}

export function getTargetRouterViewDepth (target: any) {
return target && target.$vnode && target.$vnode.data ? target.$vnode.data.routerViewDepth
: null
}
5 changes: 3 additions & 2 deletions types/router.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -354,12 +354,13 @@ interface RouteConfigMultipleViews extends _RouteConfigBase {
}

export type RouteConfig = RouteConfigSingleView | RouteConfigMultipleViews
export type VueInstance = Dictionary<Vue>

export interface RouteRecord {
path: string
regex: RegExp
components: Dictionary<Component>
instances: Dictionary<Vue>
instances: VueInstance
name?: string
parent?: RouteRecord
redirect?: RedirectOption
Expand All @@ -380,7 +381,7 @@ export interface RouteRecord {
export interface RouteRecordPublic {
path: string
components: Dictionary<Component>
instances: Dictionary<Vue>
instances: VueInstance
name?: string
redirect?: RedirectOption
meta: any
Expand Down