Template
1
0
mirror of https://github.com/un-pany/v3-admin-vite.git synced 2025-04-20 19:09:21 +08:00

refactor: 优化路由监听器,使其支持移除单个回调函数

This commit is contained in:
pany 2023-08-25 19:01:31 +08:00
parent 5113d60565
commit 41973c2013
2 changed files with 15 additions and 6 deletions

View File

@ -148,10 +148,6 @@ const closeMenu = () => {
visible.value = false
}
listenerRouteChange((route) => {
addTags(route)
})
watch(visible, (value) => {
value ? document.body.addEventListener("click", closeMenu) : document.body.removeEventListener("click", closeMenu)
})
@ -161,9 +157,17 @@ onMounted(() => {
addTags(route)
})
//#region
const callback = (route: RouteLocationNormalizedLoaded) => {
addTags(route)
}
listenerRouteChange(callback)
onBeforeUnmount(() => {
removeRouteListener()
removeRouteListener(callback)
})
//#endregion
</script>
<template>

View File

@ -23,6 +23,11 @@ export const listenerRouteChange = (callback: Callback, immediate = false) => {
}
/** 移除路由变化事件监听器 */
export const removeRouteListener = () => {
export const removeRouteListener = (callback: Callback) => {
emitter.off(key, callback as Handler)
}
/** 移除所有路由变化事件监听器 */
export const removeAllRouteListener = () => {
emitter.off(key)
}