Skip to content

Commit 4d74a2c

Browse files
committed
Merge branch 'develop' of github.com:Tencent/tdesign-vue-next-starter into develop
2 parents f0b5c2e + 12e869d commit 4d74a2c

1 file changed

Lines changed: 42 additions & 5 deletions

File tree

src/layouts/components/LayoutContent.vue

Lines changed: 42 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,19 +19,23 @@
1919
<t-dropdown
2020
trigger="context-menu"
2121
:min-column-width="128"
22-
:popup-props="{ overlayClassName: 'route-tabs-dropdown' }"
22+
:popup-props="{
23+
overlayClassName: 'route-tabs-dropdown',
24+
onVisibleChange: (visible: boolean, ctx) => handleTabMenuClick(visible, ctx, routeItem.path),
25+
visible: activeTabPath === routeItem.path,
26+
}"
2327
>
2428
<template v-if="!routeItem.isHome">
2529
{{ routeItem.title }}
2630
</template>
2731
<t-icon v-else name="home" />
2832
<template #dropdown>
29-
<t-dropdown-menu v-if="$route.path === routeItem.path">
33+
<t-dropdown-menu>
3034
<t-dropdown-item @click="() => handleRefresh(routeItem, index)">
3135
<t-icon name="refresh" />
3236
刷新
3337
</t-dropdown-item>
34-
<t-dropdown-item v-if="index > 0" @click="() => handleCloseAhead(routeItem.path, index)">
38+
<t-dropdown-item v-if="index > 1" @click="() => handleCloseAhead(routeItem.path, index)">
3539
<t-icon name="arrow-left" />
3640
关闭左侧
3741
</t-dropdown-item>
@@ -42,7 +46,7 @@
4246
<t-icon name="arrow-right" />
4347
关闭右侧
4448
</t-dropdown-item>
45-
<t-dropdown-item @click="() => handleCloseOther(routeItem.path, index)">
49+
<t-dropdown-item v-if="tabRouters.length > 2" @click="() => handleCloseOther(routeItem.path, index)">
4650
<t-icon name="close-circle" />
4751
关闭其它
4852
</t-dropdown-item>
@@ -63,7 +67,7 @@
6367
</template>
6468

6569
<script setup lang="ts">
66-
import { nextTick, computed } from 'vue';
70+
import { nextTick, ref, computed } from 'vue';
6771
import { useRoute, useRouter } from 'vue-router';
6872
import { useSettingStore, useTabsRouterStore } from '@/store';
6973
import { prefix } from '@/config/global';
@@ -79,6 +83,7 @@ const router = useRouter();
7983
const settingStore = useSettingStore();
8084
const tabsRouterStore = useTabsRouterStore();
8185
const tabRouters = computed(() => tabsRouterStore.tabRouters.filter((route) => route.isAlive || route.isHome));
86+
const activeTabPath = ref('');
8287
8388
const handleChangeCurrentTab = (path: string) => {
8489
const { tabRouters } = tabsRouterStore;
@@ -100,14 +105,46 @@ const handleRefresh = (route: TRouterInfo, routeIdx: number) => {
100105
tabsRouterStore.toggleTabRouterAlive(routeIdx);
101106
router.replace({ path: route.path, query: route.query });
102107
});
108+
activeTabPath.value = null;
103109
};
104110
const handleCloseAhead = (path: string, routeIdx: number) => {
105111
tabsRouterStore.subtractTabRouterAhead({ path, routeIdx });
112+
113+
handleOperationEffect('ahead', routeIdx);
106114
};
107115
const handleCloseBehind = (path: string, routeIdx: number) => {
108116
tabsRouterStore.subtractTabRouterBehind({ path, routeIdx });
117+
118+
handleOperationEffect('behind', routeIdx);
109119
};
110120
const handleCloseOther = (path: string, routeIdx: number) => {
111121
tabsRouterStore.subtractTabRouterOther({ path, routeIdx });
122+
123+
handleOperationEffect('other', routeIdx);
124+
};
125+
126+
// 处理非当前路由操作的副作用
127+
const handleOperationEffect = (type: 'other' | 'ahead' | 'behind', routeIndex: number) => {
128+
const currentPath = router.currentRoute.value.path;
129+
const { tabRouters } = tabsRouterStore;
130+
131+
const currentIdx = tabRouters.findIndex((i) => i.path === currentPath);
132+
// 存在三种情况需要刷新当前路由
133+
// 点击非当前路由的关闭其他、点击非当前路由的关闭左侧且当前路由小于触发路由、点击非当前路由的关闭右侧且当前路由大于触发路由
134+
const needRefreshRouter =
135+
(type === 'other' && currentIdx !== routeIndex) ||
136+
(type === 'ahead' && currentIdx < routeIndex) ||
137+
(type === 'behind' && currentIdx === -1);
138+
if (needRefreshRouter) {
139+
const nextRouteIdx = type === 'behind' ? tabRouters.length - 1 : 1;
140+
const nextRouter = tabRouters[nextRouteIdx];
141+
router.push({ path: nextRouter.path, query: nextRouter.query });
142+
}
143+
144+
activeTabPath.value = null;
145+
};
146+
const handleTabMenuClick = (visible: boolean, ctx, path: string) => {
147+
if (ctx.trigger === 'document') activeTabPath.value = null;
148+
if (visible) activeTabPath.value = path;
112149
};
113150
</script>

0 commit comments

Comments
 (0)