parent.vue 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. <template>
  2. <div class="layout-parent">
  3. <router-view v-slot="{ Component }">
  4. <transition :name="setTransitionName" mode="out-in">
  5. <keep-alive :include="getKeepAliveNames" v-if="showView">
  6. <component :is="Component" :key="state.refreshRouterViewKey" class="w100" v-show="!isIframePage" />
  7. </keep-alive>
  8. </transition>
  9. </router-view>
  10. <transition :name="setTransitionName" mode="out-in">
  11. <Iframes class="w100" v-show="isIframePage" :refreshKey="state.iframeRefreshKey" :name="setTransitionName"
  12. :list="state.iframeList" />
  13. </transition>
  14. </div>
  15. </template>
  16. <script setup lang="ts" name="layoutParentView">
  17. import { defineAsyncComponent, computed, reactive, onBeforeMount, onUnmounted, nextTick, watch, onMounted, ref, provide } from 'vue';
  18. import { useRoute, useRouter } from 'vue-router';
  19. import { storeToRefs } from 'pinia';
  20. import { useKeepALiveNames } from '/@/stores/keepAliveNames';
  21. import { useThemeConfig } from '/@/stores/themeConfig';
  22. import { Session } from '/@/utils/storage';
  23. import mittBus from '/@/utils/mitt';
  24. // 引入组件
  25. const Iframes = defineAsyncComponent(() => import('/@/layout/routerView/iframes.vue'));
  26. // 定义变量内容
  27. const route = useRoute();
  28. const router = useRouter();
  29. const storesKeepAliveNames = useKeepALiveNames();
  30. const storesThemeConfig = useThemeConfig();
  31. const { keepAliveNames, cachedViews } = storeToRefs(storesKeepAliveNames);
  32. const { themeConfig } = storeToRefs(storesThemeConfig);
  33. const state = reactive<ParentViewState>({
  34. refreshRouterViewKey: '', // 非 iframe tagsview 右键菜单刷新时
  35. iframeRefreshKey: '', // iframe tagsview 右键菜单刷新时
  36. keepAliveNameList: [],
  37. iframeList: [],
  38. });
  39. //全局依赖刷新页面
  40. const showView = ref(true)
  41. /**
  42. * 刷新页面
  43. */
  44. const refreshView = function () {
  45. showView.value = false // 通过v-if移除router-view节点
  46. nextTick(() => {
  47. showView.value = true // DOM更新后再通过v-if添加router-view节点
  48. })
  49. }
  50. provide('refreshView', refreshView)
  51. // 设置主界面切换动画
  52. const setTransitionName = computed(() => {
  53. return themeConfig.value.animation;
  54. });
  55. // 获取组件缓存列表(name值)
  56. const getKeepAliveNames = computed(() => {
  57. // console.log(cachedViews.value)
  58. return themeConfig.value.isTagsview ? cachedViews.value : state.keepAliveNameList;
  59. });
  60. // 设置 iframe 显示/隐藏
  61. const isIframePage = computed(() => {
  62. return route.meta.isIframe;
  63. });
  64. // 获取 iframe 组件列表(未进行渲染)
  65. const getIframeListRoutes = async () => {
  66. router.getRoutes().forEach((v) => {
  67. if (v.meta.isIframe) {
  68. v.meta.isIframeOpen = false;
  69. v.meta.loading = true;
  70. state.iframeList.push({ ...v });
  71. }
  72. });
  73. };
  74. // 页面加载前,处理缓存,页面刷新时路由缓存处理
  75. onBeforeMount(() => {
  76. state.keepAliveNameList = keepAliveNames.value;
  77. mittBus.on('onTagsViewRefreshRouterView', (fullPath: string) => {
  78. state.keepAliveNameList = keepAliveNames.value.filter((name: string) => route.name !== name);
  79. state.refreshRouterViewKey = '';
  80. state.iframeRefreshKey = '';
  81. nextTick(() => {
  82. state.refreshRouterViewKey = fullPath;
  83. state.iframeRefreshKey = fullPath;
  84. state.keepAliveNameList = keepAliveNames.value;
  85. });
  86. });
  87. });
  88. // 页面加载时
  89. onMounted(() => {
  90. getIframeListRoutes();
  91. // https://gitee.com/lyt-top/vue-next-admin/issues/I58U75
  92. // https://gitee.com/lyt-top/vue-next-admin/issues/I59RXK
  93. // https://gitee.com/lyt-top/vue-next-admin/pulls/40
  94. nextTick(() => {
  95. setTimeout(() => {
  96. if (themeConfig.value.isCacheTagsView) {
  97. let tagsViewArr: RouteItem[] = Session.get('tagsViewList') || [];
  98. cachedViews.value = tagsViewArr.filter((item) => item.meta?.isKeepAlive).map((item) => item.name as string);
  99. }
  100. }, 0);
  101. });
  102. });
  103. // 页面卸载时
  104. onUnmounted(() => {
  105. mittBus.off('onTagsViewRefreshRouterView', () => { });
  106. });
  107. // 监听路由变化,防止 tagsView 多标签时,切换动画消失
  108. // https://toscode.gitee.com/lyt-top/vue-next-admin/pulls/38/files
  109. watch(
  110. () => route.fullPath,
  111. () => {
  112. state.refreshRouterViewKey = decodeURI(route.fullPath);
  113. },
  114. {
  115. immediate: true,
  116. }
  117. );
  118. </script>