Skip to content

Commit ffeadc0

Browse files
committed
fix: onBeforeRouteLeave not remove (fix vuejs#3826)
1 parent f82f560 commit ffeadc0

File tree

1 file changed

+8
-8
lines changed

1 file changed

+8
-8
lines changed

src/composables/guards.js

+8-8
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,12 @@ export function onBeforeRouteLeave (guard) {
3434
return useFilteredGuard(guard, isLeaveNavigation)
3535
}
3636

37+
function registerGuard (router, guard, fn, depth) {
38+
return router.beforeEach((to, from, next) => {
39+
return fn(to, from, depth) ? guard(to, from, next) : next()
40+
})
41+
}
42+
3743
const noop = () => {}
3844
function useFilteredGuard (guard, fn) {
3945
const instance = getCurrentInstance()
@@ -56,17 +62,11 @@ function useFilteredGuard (guard, fn) {
5662
: null
5763

5864
if (depth != null) {
59-
const registerGuard = () => {
60-
return router.beforeEach((to, from, next) => {
61-
return fn(to, from, depth) ? guard(to, from, next) : next()
62-
})
63-
}
64-
65-
let removeGuard = registerGuard()
65+
let removeGuard = registerGuard(router, guard, fn, depth)
6666
onUnmounted(removeGuard)
6767

6868
onActivated(() => {
69-
removeGuard = removeGuard || registerGuard()
69+
removeGuard = removeGuard || registerGuard(router, guard, fn, depth)
7070
})
7171
onDeactivated(() => {
7272
removeGuard()

0 commit comments

Comments
 (0)