diff --git a/src/hooks/common/index.ts b/src/hooks/common/index.ts index 81bdf625..c8fe7e24 100644 --- a/src/hooks/common/index.ts +++ b/src/hooks/common/index.ts @@ -3,6 +3,5 @@ import useCreateContext from './useCreateContext'; import useRouterChange from './useRouterChange'; import useRouteParam from './useRouteParam'; import useRouteQuery from './useRouteQuery'; -import useScrollBehavior from './useScrollBehavior'; -export { useAppTitle, useCreateContext, useRouterChange, useRouteParam, useRouteQuery, useScrollBehavior }; +export { useAppTitle, useCreateContext, useRouterChange, useRouteParam, useRouteQuery }; diff --git a/src/hooks/common/useScrollBehavior.ts b/src/hooks/common/useScrollBehavior.ts deleted file mode 100644 index ca4838f4..00000000 --- a/src/hooks/common/useScrollBehavior.ts +++ /dev/null @@ -1,25 +0,0 @@ -import { ref, watch } from 'vue'; -import { useRoute } from 'vue-router'; - -export default function useScrollBehavior() { - const scrollbar = ref(null); - const route = useRoute(); - - function resetScrollBehavior() { - scrollbar.value?.scrollTo({ left: 0, top: 0 }); - } - - function resetScrollWatcher() { - watch( - () => route.name, - () => { - resetScrollBehavior(); - } - ); - } - - return { - scrollbar, - resetScrollWatcher - }; -} diff --git a/src/hooks/index.ts b/src/hooks/index.ts index 8c5da3a7..122e0e00 100644 --- a/src/hooks/index.ts +++ b/src/hooks/index.ts @@ -1,9 +1,2 @@ -export { - useAppTitle, - useCreateContext, - useRouterChange, - useRouteParam, - useRouteQuery, - useScrollBehavior -} from './common'; +export { useAppTitle, useCreateContext, useRouterChange, useRouteParam, useRouteQuery } from './common'; export { useCountDown, useSmsCode } from './business'; diff --git a/src/layouts/BasicLayout/components/GlobalTab/index.vue b/src/layouts/BasicLayout/components/GlobalTab/index.vue index d8e706ab..18ed1a80 100644 --- a/src/layouts/BasicLayout/components/GlobalTab/index.vue +++ b/src/layouts/BasicLayout/components/GlobalTab/index.vue @@ -11,7 +11,10 @@ :key="item.path" :type="app.multiTab.activeRoute === item.fullPath ? 'primary' : 'default'" class="cursor-pointer" + :closable="item.name !== ROUTE_HOME.name" + size="large" @click="handleClickTab(item.fullPath)" + @close="removeMultiTab(item.fullPath)" > {{ item.meta?.title }} @@ -28,6 +31,7 @@ import { useRoute } from 'vue-router'; import { NSpace, NTag } from 'naive-ui'; import { useThemeStore, useAppStore } from '@/store'; import { useRouterChange } from '@/hooks'; +import { ROUTE_HOME } from '@/router'; defineProps({ zIndex: { @@ -39,7 +43,7 @@ defineProps({ const route = useRoute(); const theme = useThemeStore(); const app = useAppStore(); -const { initMultiTab, addMultiTab, setActiveMultiTab, handleClickTab } = useAppStore(); +const { initMultiTab, addMultiTab, removeMultiTab, setActiveMultiTab, handleClickTab } = useAppStore(); const { toReload } = useRouterChange(); const fixedHeaderAndTab = computed(() => theme.fixedHeaderAndTab || theme.navStyle.mode === 'horizontal-mix'); diff --git a/src/layouts/BasicLayout/index.vue b/src/layouts/BasicLayout/index.vue index 48436a64..1cb18524 100644 --- a/src/layouts/BasicLayout/index.vue +++ b/src/layouts/BasicLayout/index.vue @@ -4,19 +4,24 @@
- +
- - + + - + @@ -29,15 +34,14 @@ diff --git a/src/layouts/composables/index.ts b/src/layouts/composables/index.ts new file mode 100644 index 00000000..0e7f4a4b --- /dev/null +++ b/src/layouts/composables/index.ts @@ -0,0 +1,44 @@ +import { computed, ref, watch } from 'vue'; +import { useRoute } from 'vue-router'; + +export function useRouteProps() { + const route = useRoute(); + + const props = computed(() => { + /** 路由名称 */ + const name = route.name as string; + /** 混存页面 */ + const keepAlive = Boolean(route.meta?.keepAlive); + /** 视高100% */ + const fullPage = Boolean(route.meta?.fullPage); + + return { + name, + keepAlive, + fullPage + }; + }); + + return props; +} + +/** 路由切换,重置滚动行为 */ +export function useScrollBehavior() { + const scrollbar = ref(null); + const route = useRoute(); + + function resetScrollBehavior() { + scrollbar.value?.scrollTo({ left: 0, top: 0 }); + } + + watch( + () => route.name, + () => { + resetScrollBehavior(); + } + ); + + return { + scrollbar + }; +} diff --git a/src/store/modules/app/index.ts b/src/store/modules/app/index.ts index 9e2a55d9..b29154b9 100644 --- a/src/store/modules/app/index.ts +++ b/src/store/modules/app/index.ts @@ -60,11 +60,11 @@ const appStore = defineStore({ toggleMenu() { this.menu.collapsed = !this.menu.collapsed; }, - /** 判断tab路由是否存在某个路由 */ + /** 判断页签路由是否存在某个路由 */ getIndexInTabRoutes(fullPath: string) { return this.multiTab.routes.findIndex(item => item.fullPath === fullPath); }, - /** 添加多tab的数据 */ + /** 添加多页签的数据 */ addMultiTab(route: RouteLocationNormalizedLoaded) { const { fullPath } = route; const isExist = this.getIndexInTabRoutes(fullPath) > -1; @@ -72,13 +72,24 @@ const appStore = defineStore({ this.multiTab.routes.push({ ...route }); } }, + /** 删除多页签的数据 */ + removeMultiTab(fullPath: string) { + const index = this.getIndexInTabRoutes(fullPath); + if (index > -1) { + this.multiTab.routes = this.multiTab.routes.filter(item => item.fullPath !== fullPath); + const { routes } = this.multiTab; + const activePath = routes[routes.length - 1].fullPath; + router.push(activePath); + } + }, + /** 点击单个页签tab */ handleClickTab(fullPath: string) { if (this.multiTab.activeRoute !== fullPath) { router.push(fullPath); this.setActiveMultiTab(fullPath); } }, - /** 设置当前路由对应的tab为激活状态 */ + /** 设置当前路由对应的页签为激活状态 */ setActiveMultiTab(fullPath: string) { this.multiTab.activeRoute = fullPath; },