parent.vue 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. <template>
  2. <div class="h100">
  3. <router-view v-slot="{ Component }">
  4. <transition :name="setTransitionName" mode="out-in">
  5. <keep-alive :include="keepAliveNameList">
  6. <component :is="Component" :key="refreshRouterViewKey" class="w100" :style="{ minHeight }" />
  7. </keep-alive>
  8. </transition>
  9. </router-view>
  10. </div>
  11. </template>
  12. <script lang="ts">
  13. import { computed, defineComponent, toRefs, reactive, getCurrentInstance, onBeforeMount, onUnmounted, nextTick, watch } from 'vue';
  14. import { useRoute } from 'vue-router';
  15. import { useStore } from '/@/store/index';
  16. // 定义接口来定义对象的类型
  17. interface ParentViewState {
  18. refreshRouterViewKey: null | string;
  19. keepAliveNameList: string[];
  20. }
  21. export default defineComponent({
  22. name: 'layoutParentView',
  23. props: {
  24. minHeight: {
  25. type: String,
  26. default: '',
  27. },
  28. },
  29. setup() {
  30. const { proxy } = <any>getCurrentInstance();
  31. const route = useRoute();
  32. const store = useStore();
  33. const state = reactive<ParentViewState>({
  34. refreshRouterViewKey: null,
  35. keepAliveNameList: [],
  36. });
  37. // 设置主界面切换动画
  38. const setTransitionName = computed(() => {
  39. return store.state.themeConfig.themeConfig.animation;
  40. });
  41. // 获取布局配置信息
  42. const getThemeConfig = computed(() => {
  43. return store.state.themeConfig.themeConfig;
  44. });
  45. // 获取组件缓存列表(name值)
  46. const getKeepAliveNames = computed(() => {
  47. return store.state.keepAliveNames.keepAliveNames;
  48. });
  49. // 页面加载前,处理缓存,页面刷新时路由缓存处理
  50. onBeforeMount(() => {
  51. state.keepAliveNameList = getKeepAliveNames.value;
  52. proxy.mittBus.on('onTagsViewRefreshRouterView', (fullPath: string) => {
  53. state.keepAliveNameList = getKeepAliveNames.value.filter((name: string) => route.name !== name);
  54. state.refreshRouterViewKey = null;
  55. nextTick(() => {
  56. state.refreshRouterViewKey = fullPath;
  57. state.keepAliveNameList = getKeepAliveNames.value;
  58. });
  59. });
  60. });
  61. // 页面卸载时
  62. onUnmounted(() => {
  63. proxy.mittBus.off('onTagsViewRefreshRouterView');
  64. });
  65. // 监听路由变化,防止 tagsView 多标签时,切换动画消失
  66. watch(
  67. () => route.fullPath,
  68. () => {
  69. state.refreshRouterViewKey = route.fullPath;
  70. }
  71. );
  72. return {
  73. getThemeConfig,
  74. getKeepAliveNames,
  75. setTransitionName,
  76. ...toRefs(state),
  77. };
  78. },
  79. });
  80. </script>