wujingjing
2024-07-19 82210fced4310307d5a2b03470d16a8ec98331ab
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
34
35
36
import { onActivated, onBeforeUnmount, onDeactivated, onMounted, onUnmounted, ref } from 'vue';
import router from '../router';
 
/**
 * 开启路由缓存页面,离开时,和进入时取消/开启订阅事件
 * @returns
 */
export const usePageDisplay = (pageShow?: () => void, pageHide?: () => void, needExecutedMounted = true) => {
    const haveExecutedMounted = ref(!needExecutedMounted);
    const currentRoute = router.currentRoute.value;
 
    const isKeepAlive = currentRoute.meta.isKeepAlive ?? false;
    if (isKeepAlive) {
        onActivated(() => {
            if (!haveExecutedMounted.value) {
                return;
            }
            pageShow?.();
        });
 
        onDeactivated(() => {
            pageHide?.();
        });
    } else {
        onMounted(() => {
            pageShow?.();
        });
        onBeforeUnmount(() => {
            pageHide?.();
        });
    }
 
    return {
        haveExecutedMounted,
    };
};