1
0

index.tsx 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. import type { TabContentProps } from './tab.data';
  2. import type { TabItem } from '/@/store/modules/tab';
  3. import type { AppRouteRecordRaw } from '/@/router/types';
  4. import {
  5. defineComponent,
  6. watch,
  7. computed,
  8. // ref,
  9. unref,
  10. onMounted,
  11. } from 'vue';
  12. import { Tabs } from 'ant-design-vue';
  13. import TabContent from './TabContent';
  14. import { useGo } from '/@/hooks/web/usePage';
  15. import { TabContentEnum } from './tab.data';
  16. import { useRouter } from 'vue-router';
  17. import { tabStore } from '/@/store/modules/tab';
  18. import { closeTab } from './useTabDropdown';
  19. import router from '/@/router';
  20. import { useTabs } from '/@/hooks/web/useTabs';
  21. import { PageEnum } from '/@/enums/pageEnum';
  22. import './index.less';
  23. export default defineComponent({
  24. name: 'MultiTabs',
  25. setup() {
  26. let isAddAffix = false;
  27. const go = useGo();
  28. const { currentRoute } = useRouter();
  29. const { addTab, activeKeyRef } = useTabs();
  30. onMounted(() => {
  31. addTab(unref(currentRoute).path as PageEnum);
  32. });
  33. // 当前激活tab
  34. // const activeKeyRef = ref<string>('');
  35. // 当前tab列表
  36. const getTabsState = computed(() => {
  37. return tabStore.getTabsState;
  38. });
  39. if (!isAddAffix) {
  40. addAffixTabs();
  41. isAddAffix = true;
  42. }
  43. watch(
  44. () => unref(currentRoute).path,
  45. (path) => {
  46. if (activeKeyRef.value !== path) {
  47. activeKeyRef.value = path;
  48. }
  49. // 监听路由的话虽然可以,但是路由切换的时间会造成卡顿现象?
  50. // 使用useTab的addTab的话,当用户手动调转,需要自行调用addTab
  51. // tabStore.commitAddTab((unref(currentRoute) as unknown) as AppRouteRecordRaw);
  52. },
  53. {
  54. immediate: true,
  55. }
  56. );
  57. /**
  58. * @description: 过滤所有固定路由
  59. */
  60. function filterAffixTabs(routes: AppRouteRecordRaw[]) {
  61. const tabs: TabItem[] = [];
  62. routes &&
  63. routes.forEach((route) => {
  64. if (route.meta && route.meta.affix) {
  65. tabs.push({
  66. path: route.path,
  67. name: route.name,
  68. meta: { ...route.meta },
  69. });
  70. }
  71. });
  72. return tabs;
  73. }
  74. /**
  75. * @description: 设置固定tabs
  76. */
  77. function addAffixTabs(): void {
  78. const affixTabs = filterAffixTabs((router.getRoutes() as unknown) as AppRouteRecordRaw[]);
  79. for (const tab of affixTabs) {
  80. tabStore.commitAddTab(tab);
  81. }
  82. }
  83. // tab切换
  84. function handleChange(activeKey: any) {
  85. activeKeyRef.value = activeKey;
  86. go(activeKey, false);
  87. }
  88. // 关闭当前ab
  89. function handleEdit(targetKey: string) {
  90. // 新增操作隐藏,目前只使用删除操作
  91. const index = unref(getTabsState).findIndex((item) => item.path === targetKey);
  92. index !== -1 && closeTab(unref(getTabsState)[index]);
  93. }
  94. function renderQuick() {
  95. const tabContentProps: TabContentProps = {
  96. tabItem: (currentRoute as unknown) as AppRouteRecordRaw,
  97. type: TabContentEnum.EXTRA_TYPE,
  98. trigger: ['click', 'contextmenu'],
  99. };
  100. return (
  101. <span>
  102. <TabContent {...tabContentProps} />
  103. </span>
  104. );
  105. }
  106. function renderTabs() {
  107. return unref(getTabsState).map((item: TabItem) => {
  108. return (
  109. <Tabs.TabPane key={item.path} closable={!(item && item.meta && item.meta.affix)}>
  110. {{
  111. tab: () => <TabContent tabItem={item} />,
  112. }}
  113. </Tabs.TabPane>
  114. );
  115. });
  116. }
  117. return () => {
  118. return (
  119. <div class="multiple-tabs">
  120. <Tabs
  121. type="editable-card"
  122. size="small"
  123. hideAdd={true}
  124. tabBarGutter={2}
  125. activeKey={unref(activeKeyRef)}
  126. onChange={handleChange}
  127. onEdit={handleEdit}
  128. >
  129. {{
  130. default: () => renderTabs(),
  131. tabBarExtraContent: () => renderQuick(),
  132. }}
  133. </Tabs>
  134. </div>
  135. );
  136. };
  137. },
  138. });