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

fix: onBeforeRouteLeave not remove (fix #3826) #3875

Open
wants to merge 3 commits into
base: dev
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
20 changes: 16 additions & 4 deletions 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'

Expand Down Expand Up @@ -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 = () => {
wynn719 marked this conversation as resolved.
Show resolved Hide resolved
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
posva marked this conversation as resolved.
Show resolved Hide resolved
})
posva marked this conversation as resolved.
Show resolved Hide resolved

return removeGuard
}

Expand Down