From 34599901c77f4f9381be95b56c9dcd2fec414465 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=83=A1=E5=B0=91=E7=BF=94?= <971366405@qq.com> Date: Tue, 25 Jun 2024 11:57:28 +0800 Subject: [PATCH] =?UTF-8?q?feat(tagView):=20:sparkles:=20=E5=A2=9E?= =?UTF-8?q?=E5=8A=A0=E5=85=B3=E9=97=AD=E5=BD=93=E5=89=8DtagView=E6=96=B9?= =?UTF-8?q?=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 增加关闭当前tagView方法 --- src/layout/components/TagsView/index.vue | 32 ++++------------- src/store/modules/tagsView.ts | 44 +++++++++++++++++++++++- 2 files changed, 49 insertions(+), 27 deletions(-) diff --git a/src/layout/components/TagsView/index.vue b/src/layout/components/TagsView/index.vue index 7d2096fe..031d8641 100644 --- a/src/layout/components/TagsView/index.vue +++ b/src/layout/components/TagsView/index.vue @@ -9,7 +9,7 @@ ref="tagRef" v-for="tag in visitedViews" :key="tag.fullPath" - :class="'tags-item ' + (isActive(tag) ? 'active' : '')" + :class="'tags-item ' + (tagsViewStore.isActive(tag) ? 'active' : '')" :to="{ path: tag.path, query: tag.query }" @click.middle="!isAffix(tag) ? closeSelectedTag(tag) : ''" @contextmenu.prevent="openContentMenu(tag, $event)" @@ -190,10 +190,6 @@ function moveToCurrentTag() { }); } -function isActive(tag: TagView) { - return tag.path === route.path; -} - function isAffix(tag: TagView) { return tag?.affix; } @@ -228,26 +224,10 @@ function refreshSelectedTag(view: TagView) { }); } -function toLastView(visitedViews: TagView[], view?: TagView) { - const latestView = visitedViews.slice(-1)[0]; - if (latestView && latestView.fullPath) { - router.push(latestView.fullPath); - } else { - // now the default is to redirect to the home page if there is no tags-view, - // you can adjust it according to your needs. - if (view?.name === "Dashboard") { - // to reload home page - router.replace("/redirect" + view.fullPath); - } else { - router.push("/"); - } - } -} - function closeSelectedTag(view: TagView) { tagsViewStore.delView(view).then((res: any) => { - if (isActive(view)) { - toLastView(res.visitedViews, view); + if (tagsViewStore.isActive(view)) { + tagsViewStore.toLastView(res.visitedViews, view); } }); } @@ -255,14 +235,14 @@ function closeSelectedTag(view: TagView) { function closeLeftTags() { tagsViewStore.delLeftViews(selectedTag.value).then((res: any) => { if (!res.visitedViews.find((item: any) => item.path === route.path)) { - toLastView(res.visitedViews); + tagsViewStore.toLastView(res.visitedViews); } }); } function closeRightTags() { tagsViewStore.delRightViews(selectedTag.value).then((res: any) => { if (!res.visitedViews.find((item: any) => item.path === route.path)) { - toLastView(res.visitedViews); + tagsViewStore.toLastView(res.visitedViews); } }); } @@ -276,7 +256,7 @@ function closeOtherTags() { function closeAllTags(view: TagView) { tagsViewStore.delAllViews().then((res: any) => { - toLastView(res.visitedViews, view); + tagsViewStore.toLastView(res.visitedViews, view); }); } diff --git a/src/store/modules/tagsView.ts b/src/store/modules/tagsView.ts index 9e66c10e..a92e4fe8 100644 --- a/src/store/modules/tagsView.ts +++ b/src/store/modules/tagsView.ts @@ -1,7 +1,8 @@ export const useTagsViewStore = defineStore("tagsView", () => { const visitedViews = ref([]); const cachedViews = ref([]); - + const router = useRouter(); + const route = useRoute(); /** * 添加已访问视图到已访问视图列表中 */ @@ -189,6 +190,44 @@ export const useTagsViewStore = defineStore("tagsView", () => { }); } + /** + * 关闭当前tagView + */ + function closeCurrentView() { + const tags: TagView = { + name: route.name as string, + title: route.meta.title as string, + path: route.path, + fullPath: route.fullPath, + affix: route.meta?.affix, + keepAlive: route.meta?.keepAlive, + query: route.query, + }; + delView(tags).then((res: any) => { + if (isActive(tags)) { + toLastView(res.visitedViews, tags); + } + }); + } + function isActive(tag: TagView) { + return tag.path === route.path; + } + + function toLastView(visitedViews: TagView[], view?: TagView) { + const latestView = visitedViews.slice(-1)[0]; + if (latestView && latestView.fullPath) { + router.push(latestView.fullPath); + } else { + // now the default is to redirect to the home page if there is no tags-view, + // you can adjust it according to your needs. + if (view?.name === "Dashboard") { + // to reload home page + router.replace("/redirect" + view.fullPath); + } else { + router.push("/"); + } + } + } return { visitedViews, cachedViews, @@ -207,5 +246,8 @@ export const useTagsViewStore = defineStore("tagsView", () => { delAllViews, delAllVisitedViews, delAllCachedViews, + closeCurrentView, + isActive, + toLastView, }; }); -- Gitee