index.vue 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. <script setup lang="ts">
  2. import { ref, computed } from "vue";
  3. import { transformI18n } from "/@/plugins/i18n";
  4. import { TreeSelect } from "@pureadmin/components";
  5. import { useMultiTagsStoreHook } from "/@/store/modules/multiTags";
  6. import { usePermissionStoreHook } from "/@/store/modules/permission";
  7. import {
  8. deleteChildren,
  9. appendFieldByUniqueId,
  10. getNodeByUniqueId
  11. } from "@pureadmin/utils";
  12. import { useDetail } from "./hooks";
  13. defineOptions({
  14. name: "Tabs"
  15. });
  16. const { toDetail, router } = useDetail();
  17. let treeData = computed(() => {
  18. return appendFieldByUniqueId(
  19. deleteChildren(usePermissionStoreHook().menusTree),
  20. 0,
  21. { disabled: true }
  22. );
  23. });
  24. const value = ref<string[]>([]);
  25. let multiTags = computed(() => {
  26. return useMultiTagsStoreHook()?.multiTags;
  27. });
  28. function onCloseTags() {
  29. value.value.forEach(uniqueId => {
  30. let currentPath =
  31. getNodeByUniqueId(treeData.value, uniqueId).redirect ??
  32. getNodeByUniqueId(treeData.value, uniqueId).path;
  33. useMultiTagsStoreHook().handleTags("splice", currentPath);
  34. if (currentPath === "/tabs/index")
  35. // @ts-expect-error
  36. router.push({ path: multiTags.value[multiTags.value.length - 1].path });
  37. });
  38. }
  39. </script>
  40. <template>
  41. <el-card>
  42. <template #header>
  43. <div>标签页复用,超出限制自动关闭(使用场景: 动态路由)</div>
  44. </template>
  45. <el-button v-for="index in 6" :key="index" @click="toDetail(index)">
  46. 打开{{ index }}详情页
  47. </el-button>
  48. <el-divider />
  49. <TreeSelect
  50. class="w-300px"
  51. v-model:value="value"
  52. show-search
  53. :dropdown-style="{ maxHeight: '400px', overflow: 'auto' }"
  54. placeholder="请选择要关闭的标签"
  55. :fieldNames="{
  56. children: 'children',
  57. key: 'uniqueId',
  58. value: 'uniqueId'
  59. }"
  60. allow-clear
  61. multiple
  62. tree-default-expand-all
  63. :tree-data="treeData"
  64. >
  65. <template #tagRender="{ closable, onClose, option }">
  66. <el-tag class="mr-3px" :closable="closable" @close="onClose">
  67. {{ transformI18n(option.meta.title) }}
  68. </el-tag>
  69. </template>
  70. <template #title="{ data }">
  71. <span>{{ transformI18n(data.meta.title) }}</span>
  72. </template>
  73. </TreeSelect>
  74. <el-button class="ml-2" @click="onCloseTags">关闭标签</el-button>
  75. <br />
  76. <p class="mt-4">
  77. 注意:此demo并未开启标签页缓存,如果需要在
  78. <span class="text-red-500">刷新页面</span>
  79. 的时候同时
  80. <span class="text-red-500">保留标签页的显示</span>
  81. 或者
  82. <span class="text-red-500">保留url的参数</span>
  83. ,那么就需要开启标签页持久化。
  84. <br />
  85. 开启方式:在页面最右上角有个设置的小图标,点进去,会看到项目配置面板,找到标签页持久化开启即可。
  86. </p>
  87. </el-card>
  88. </template>