Skip to content

Commit f82f560

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

File tree

1 file changed

+16
-4
lines changed

1 file changed

+16
-4
lines changed

src/composables/guards.js

+16-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { getCurrentInstance, onUnmounted } from 'vue'
1+
import { getCurrentInstance, onUnmounted, onActivated, onDeactivated } from 'vue'
22
import { throwNoCurrentInstance } from './utils'
33
import { useRouter } from './globals'
44

@@ -56,11 +56,23 @@ function useFilteredGuard (guard, fn) {
5656
: null
5757

5858
if (depth != null) {
59-
const removeGuard = router.beforeEach((to, from, next) => {
60-
return fn(to, from, depth) ? guard(to, from, next) : next()
61-
})
59+
const registerGuard = () => {
60+
return router.beforeEach((to, from, next) => {
61+
return fn(to, from, depth) ? guard(to, from, next) : next()
62+
})
63+
}
6264

65+
let removeGuard = registerGuard()
6366
onUnmounted(removeGuard)
67+
68+
onActivated(() => {
69+
removeGuard = removeGuard || registerGuard()
70+
})
71+
onDeactivated(() => {
72+
removeGuard()
73+
removeGuard = null // reset removeGuard
74+
})
75+
6476
return removeGuard
6577
}
6678

0 commit comments

Comments
 (0)