index.vue 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. router.push({
  36. path: multiTags.value[(multiTags as any).value.length - 1].path
  37. });
  38. });
  39. }
  40. </script>
  41. <template>
  42. <el-card>
  43. <template #header>
  44. <div>标签页复用,超出限制自动关闭(使用场景: 动态路由)</div>
  45. </template>
  46. <el-button v-for="index in 6" :key="index" @click="toDetail(index)">
  47. 打开{{ index }}详情页
  48. </el-button>
  49. <el-divider />
  50. <TreeSelect
  51. class="w-300px"
  52. v-model:value="value"
  53. show-search
  54. :dropdown-style="{ maxHeight: '400px', overflow: 'auto' }"
  55. placeholder="请选择要关闭的标签"
  56. :fieldNames="{
  57. children: 'children',
  58. key: 'uniqueId',
  59. value: 'uniqueId'
  60. }"
  61. allow-clear
  62. multiple
  63. tree-default-expand-all
  64. :tree-data="treeData"
  65. >
  66. <template #tagRender="{ closable, onClose, option }">
  67. <el-tag class="mr-3px" :closable="closable" @close="onClose">
  68. {{ transformI18n(option.meta.title) }}
  69. </el-tag>
  70. </template>
  71. <template #title="{ data }">
  72. <span>{{ transformI18n(data.meta.title) }}</span>
  73. </template>
  74. </TreeSelect>
  75. <el-button class="ml-2" @click="onCloseTags">关闭标签</el-button>
  76. <br />
  77. <p class="mt-4">
  78. 注意:此demo并未开启标签页缓存,如果需要在
  79. <span class="text-red-500">刷新页面</span>
  80. 的时候同时
  81. <span class="text-red-500">保留标签页的显示</span>
  82. 或者
  83. <span class="text-red-500">保留url的参数</span>
  84. ,那么就需要开启标签页持久化。
  85. <br />
  86. 开启方式:在页面最右上角有个设置的小图标,点进去,会看到项目配置面板,找到标签页持久化开启即可。
  87. </p>
  88. </el-card>
  89. </template>