yangyin
2024-09-06 cf5c3cde7d0dbec44d4c2f7bea87c4d5362accb0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import { onActivated, onBeforeUnmount, onDeactivated, onMounted } from 'vue';
import router from '/@/router';
 
/**
 * 页面显示/隐藏回调
 * 不管当前路由是否缓存,都可以使用此钩子
 * @param pageShow
 * @param pageHide
 * @returns
 */
export const usePageDisplay = (pageShow?: () => void, pageHide?: () => void) => {
    const currentRoute = router.currentRoute.value;
    // 当前页面是否开启路由缓存
    const isKeepAlive = currentRoute.meta.isKeepAlive ?? false;
    if (isKeepAlive) {
        onActivated(() => {
            pageShow?.();
        });
 
        onDeactivated(() => {
            pageHide?.();
        });
    } else {
        onMounted(() => {
            pageShow?.();
        });
 
        // 方便在此操作 dom,取消监听,unmounted 时 dom 已销毁
        onBeforeUnmount(() => {
            pageHide?.();
        });
    }
};