wujingjing
2024-10-30 03080e12de7de3fec89b2f77fcf9f73c02bc6cf0
src/hooks/usePageDisplay.ts
@@ -1,23 +1,36 @@
import { onActivated, onDeactivated, ref } from 'vue';
import { onActivated, onBeforeUnmount, onDeactivated, onMounted, onUnmounted, ref } from 'vue';
import router from '../router';
/**
 * 开启路由缓存页面,离开时,和进入时取消/开启订阅事件
 * @returns
 */
export const usePageDisplay = (pageShow?: () => void, pageHide?: () => void) => {
   const haveExecutedMounted = ref(false);
   onActivated(() => {
      if (!haveExecutedMounted.value) {
         return;
      }
      pageShow?.();
   });
export const usePageDisplay = (pageShow?: () => void, pageHide?: () => void, needExecutedMounted = true) => {
   const haveExecutedMounted = ref(!needExecutedMounted);
   const currentRoute = router.currentRoute.value;
   onDeactivated(() => {
      pageHide?.();
   });
   const isKeepAlive = currentRoute.meta.isKeepAlive ?? false;
   if (isKeepAlive) {
      onActivated(() => {
         if (!haveExecutedMounted.value) {
            return;
         }
         pageShow?.();
      });
      onDeactivated(() => {
         pageHide?.();
      });
   } else {
      onMounted(() => {
         pageShow?.();
      });
      onBeforeUnmount(() => {
         pageHide?.();
      });
   }
   return {
        haveExecutedMounted
    };
      haveExecutedMounted,
   };
};