You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

79 lines
2.2 KiB

  1. <script setup lang="ts">
  2. import { getConfig } from "@/config";
  3. import { useMultiTagsStoreHook } from "@/store/modules/multiTags";
  4. import { type Component, shallowRef, watch, computed } from "vue";
  5. import { type RouteRecordRaw, RouteLocationNormalizedLoaded } from "vue-router";
  6. import { useMultiFrame } from "@/layout/components/keepAliveFrame/useMultiFrame";
  7. const props = defineProps<{
  8. currRoute: RouteLocationNormalizedLoaded;
  9. currComp: Component;
  10. }>();
  11. const compList = shallowRef([]);
  12. const { setMap, getMap, MAP, delMap } = useMultiFrame();
  13. const keep = computed(() => {
  14. return (
  15. getConfig().KeepAlive &&
  16. props.currRoute.meta?.keepAlive &&
  17. !!props.currRoute.meta?.frameSrc
  18. );
  19. });
  20. // 避免重新渲染 frameView
  21. const normalComp = computed(() => !keep.value && props.currComp);
  22. watch(useMultiTagsStoreHook().multiTags, (tags: any) => {
  23. if (!Array.isArray(tags) || !keep.value) {
  24. return;
  25. }
  26. const iframeTags = tags.filter(i => i.meta?.frameSrc);
  27. // tags必须是小于MAP,才是做了关闭动作,因为MAP插入的顺序在tags变化后发生
  28. if (iframeTags.length < MAP.size) {
  29. for (const i of MAP.keys()) {
  30. if (!tags.some(s => s.path === i)) {
  31. delMap(i);
  32. compList.value = getMap();
  33. }
  34. }
  35. }
  36. });
  37. watch(
  38. () => props.currRoute.fullPath,
  39. path => {
  40. const multiTags = useMultiTagsStoreHook().multiTags as RouteRecordRaw[];
  41. const iframeTags = multiTags.filter(i => i.meta?.frameSrc);
  42. if (keep.value) {
  43. if (iframeTags.length !== MAP.size) {
  44. const sameKey = [...MAP.keys()].find(i => path === i);
  45. if (!sameKey) {
  46. // 添加缓存
  47. setMap(path, props.currComp);
  48. }
  49. }
  50. }
  51. if (MAP.size > 0) {
  52. compList.value = getMap();
  53. }
  54. },
  55. {
  56. immediate: true
  57. }
  58. );
  59. </script>
  60. <template>
  61. <template v-for="[fullPath, Comp] in compList" :key="fullPath">
  62. <div v-show="fullPath === props.currRoute.fullPath" class="w-full h-full">
  63. <slot
  64. :fullPath="fullPath"
  65. :Comp="Comp"
  66. :frameInfo="{ frameSrc: currRoute.meta?.frameSrc, fullPath }"
  67. />
  68. </div>
  69. </template>
  70. <div v-show="!keep" class="w-full h-full">
  71. <slot :Comp="normalComp" :fullPath="props.currRoute.fullPath" frameInfo />
  72. </div>
  73. </template>