Skip to content

Commit

Permalink
fix: onBeforeRouteLeave not remove (fix vuejs#3826)
Browse files Browse the repository at this point in the history
  • Loading branch information
wynn719 committed Nov 16, 2023
1 parent f82f560 commit ffeadc0
Showing 1 changed file with 8 additions and 8 deletions.
16 changes: 8 additions & 8 deletions src/composables/guards.js
Expand Up @@ -34,6 +34,12 @@ export function onBeforeRouteLeave (guard) {
return useFilteredGuard(guard, isLeaveNavigation)
}

function registerGuard (router, guard, fn, depth) {
return router.beforeEach((to, from, next) => {
return fn(to, from, depth) ? guard(to, from, next) : next()
})
}

const noop = () => {}
function useFilteredGuard (guard, fn) {
const instance = getCurrentInstance()
Expand All @@ -56,17 +62,11 @@ function useFilteredGuard (guard, fn) {
: null

if (depth != null) {
const registerGuard = () => {
return router.beforeEach((to, from, next) => {
return fn(to, from, depth) ? guard(to, from, next) : next()
})
}

let removeGuard = registerGuard()
let removeGuard = registerGuard(router, guard, fn, depth)
onUnmounted(removeGuard)

onActivated(() => {
removeGuard = removeGuard || registerGuard()
removeGuard = removeGuard || registerGuard(router, guard, fn, depth)
})
onDeactivated(() => {
removeGuard()
Expand Down

0 comments on commit ffeadc0

Please sign in to comment.