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 0395bd8 commit f82f560
Showing 1 changed file with 16 additions and 4 deletions.
20 changes: 16 additions & 4 deletions src/composables/guards.js
Original file line number Diff line number Diff line change
@@ -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 = () => {
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
}

Expand Down

0 comments on commit f82f560

Please sign in to comment.