index.vue 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. <template>
  2. <div class="app-container" v-loading="loading">
  3. <div class="tabs-container">
  4. <el-tabs v-model="currentTab" class="paper-tabs">
  5. <el-tab-pane v-for="t in tabs" :label="t.label" :name="t.name" :key="t.name">
  6. <component :is="t.page" v-if="t.visited" :key="t.name" :ref="el => setComponentRef(t.name, el)"/>
  7. </el-tab-pane>
  8. </el-tabs>
  9. <el-button
  10. type="primary"
  11. :icon="Refresh"
  12. circle
  13. size="small"
  14. @click="handleRefresh"
  15. class="refresh-btn"
  16. title="刷新当前页面数据"
  17. />
  18. </div>
  19. </div>
  20. </template>
  21. <script setup name="PaperIndex">
  22. import {ref, watch, markRaw} from "vue";
  23. import {useProvideGlobalLoading} from "@/views/hooks/useGlobalLoading.js";
  24. import PaperExactIntelligent from "@/views/dz/papers/components/paper-exact-intelligent.vue";
  25. import PaperFullIntelligent from "@/views/dz/papers/components/paper-full-intelligent.vue";
  26. import PaperExactHand from "@/views/dz/papers/components/paper-exact-hand.vue";
  27. import PaperFullHand from "@/views/dz/papers/components/paper-full-hand.vue";
  28. import {Refresh} from "@element-plus/icons-vue";
  29. const {loading} = useProvideGlobalLoading()
  30. const tabs = ref([{
  31. name: 'ExactIntelligent',
  32. label: '定向智能',
  33. page: markRaw(PaperExactIntelligent) ,
  34. visited: true
  35. }, {
  36. name: 'FullIntelligent',
  37. label: '全量智能',
  38. page: markRaw(PaperFullIntelligent),
  39. visited: false
  40. }, {
  41. name: 'ExactHand',
  42. label: '定向手动',
  43. page: markRaw(PaperExactHand),
  44. visited: false
  45. }, {
  46. name: 'FullHand',
  47. label: '全量手动',
  48. page: markRaw(PaperFullHand),
  49. visited: false
  50. }])
  51. const currentTab = ref('ExactIntelligent')
  52. // 存储组件实例的引用
  53. const componentRefs = ref({})
  54. // 设置组件引用
  55. const setComponentRef = (name, el) => {
  56. if (el) {
  57. // 处理 ref 可能是数组的情况
  58. const component = Array.isArray(el) ? el[0] : el
  59. if (component) {
  60. componentRefs.value[name] = component
  61. }
  62. } else {
  63. // 组件卸载时清除引用
  64. delete componentRefs.value[name]
  65. }
  66. }
  67. // 刷新当前 tab 的数据
  68. const handleRefresh = () => {
  69. const currentComponent = componentRefs.value[currentTab.value]
  70. if (currentComponent && typeof currentComponent.refresh === 'function') {
  71. currentComponent.refresh()
  72. } else {
  73. console.warn('当前 tab 组件不支持刷新功能')
  74. }
  75. }
  76. watch(currentTab, tabName => {
  77. // 通过visited=true 延迟渲染,组件只创建一次,切换时不会重新创建
  78. const tab = tabs.value.find(t => t.name == tabName)
  79. if (tab) tab.visited = true
  80. })
  81. </script>
  82. <style scoped>
  83. .tabs-container {
  84. position: relative;
  85. }
  86. .paper-tabs {
  87. width: calc(100% - 50px);
  88. }
  89. .refresh-btn {
  90. position: absolute;
  91. right: 0;
  92. top: 8px;
  93. z-index: 10;
  94. }
  95. </style>