From f82f56000c90dcecc0c29eea9c45a00b97907afa Mon Sep 17 00:00:00 2001 From: wynn719 <762671261@qq.com> Date: Thu, 16 Nov 2023 17:17:47 +0800 Subject: [PATCH 1/3] fix: onBeforeRouteLeave not remove (fix #3826) --- src/composables/guards.js | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/src/composables/guards.js b/src/composables/guards.js index 6312e9401..7cdc9e952 100644 --- a/src/composables/guards.js +++ b/src/composables/guards.js @@ -1,4 +1,4 @@ -import { getCurrentInstance, onUnmounted } from 'vue' +import { getCurrentInstance, onUnmounted, onActivated, onDeactivated } from 'vue' import { throwNoCurrentInstance } from './utils' import { useRouter } from './globals' @@ -56,11 +56,23 @@ function useFilteredGuard (guard, fn) { : null if (depth != null) { - const removeGuard = router.beforeEach((to, from, next) => { - return fn(to, from, depth) ? guard(to, from, next) : next() - }) + const registerGuard = () => { + return router.beforeEach((to, from, next) => { + return fn(to, from, depth) ? guard(to, from, next) : next() + }) + } + let removeGuard = registerGuard() onUnmounted(removeGuard) + + onActivated(() => { + removeGuard = removeGuard || registerGuard() + }) + onDeactivated(() => { + removeGuard() + removeGuard = null // reset removeGuard + }) + return removeGuard } From ffeadc0289b0d834eaa6bd28988933706858227c Mon Sep 17 00:00:00 2001 From: wynn719 <762671261@qq.com> Date: Thu, 16 Nov 2023 19:23:49 +0800 Subject: [PATCH 2/3] fix: onBeforeRouteLeave not remove (fix #3826) --- src/composables/guards.js | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/composables/guards.js b/src/composables/guards.js index 7cdc9e952..3c963570b 100644 --- a/src/composables/guards.js +++ b/src/composables/guards.js @@ -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() @@ -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() From b382a4f536d9ea857f746c28c85f684456a65db7 Mon Sep 17 00:00:00 2001 From: Eduardo San Martin Morote Date: Fri, 17 Nov 2023 15:31:16 +0100 Subject: [PATCH 3/3] Apply suggestions from code review --- src/composables/guards.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/composables/guards.js b/src/composables/guards.js index 3c963570b..e88ea5a8a 100644 --- a/src/composables/guards.js +++ b/src/composables/guards.js @@ -35,9 +35,9 @@ export function onBeforeRouteLeave (guard) { } function registerGuard (router, guard, fn, depth) { - return router.beforeEach((to, from, next) => { - return fn(to, from, depth) ? guard(to, from, next) : next() - }) + return router.beforeEach((to, from, next) => + fn(to, from, depth) ? guard(to, from, next) : next() + ) } const noop = () => {} @@ -70,7 +70,7 @@ function useFilteredGuard (guard, fn) { }) onDeactivated(() => { removeGuard() - removeGuard = null // reset removeGuard + removeGuard = null }) return removeGuard