| | |
| | | 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, |
| | | }; |
| | | }; |