fix(projects): 修复页面缓存,添加多页签删除

This commit is contained in:
Soybean 2021-09-17 23:44:28 +08:00
parent f29bc05dd9
commit 248937479c
8 changed files with 86 additions and 75 deletions

View File

@ -3,6 +3,5 @@ import useCreateContext from './useCreateContext';
import useRouterChange from './useRouterChange'; import useRouterChange from './useRouterChange';
import useRouteParam from './useRouteParam'; import useRouteParam from './useRouteParam';
import useRouteQuery from './useRouteQuery'; import useRouteQuery from './useRouteQuery';
import useScrollBehavior from './useScrollBehavior';
export { useAppTitle, useCreateContext, useRouterChange, useRouteParam, useRouteQuery, useScrollBehavior }; export { useAppTitle, useCreateContext, useRouterChange, useRouteParam, useRouteQuery };

View File

@ -1,25 +0,0 @@
import { ref, watch } from 'vue';
import { useRoute } from 'vue-router';
export default function useScrollBehavior() {
const scrollbar = ref<HTMLElement | null>(null);
const route = useRoute();
function resetScrollBehavior() {
scrollbar.value?.scrollTo({ left: 0, top: 0 });
}
function resetScrollWatcher() {
watch(
() => route.name,
() => {
resetScrollBehavior();
}
);
}
return {
scrollbar,
resetScrollWatcher
};
}

View File

@ -1,9 +1,2 @@
export { export { useAppTitle, useCreateContext, useRouterChange, useRouteParam, useRouteQuery } from './common';
useAppTitle,
useCreateContext,
useRouterChange,
useRouteParam,
useRouteQuery,
useScrollBehavior
} from './common';
export { useCountDown, useSmsCode } from './business'; export { useCountDown, useSmsCode } from './business';

View File

@ -11,7 +11,10 @@
:key="item.path" :key="item.path"
:type="app.multiTab.activeRoute === item.fullPath ? 'primary' : 'default'" :type="app.multiTab.activeRoute === item.fullPath ? 'primary' : 'default'"
class="cursor-pointer" class="cursor-pointer"
:closable="item.name !== ROUTE_HOME.name"
size="large"
@click="handleClickTab(item.fullPath)" @click="handleClickTab(item.fullPath)"
@close="removeMultiTab(item.fullPath)"
> >
{{ item.meta?.title }} {{ item.meta?.title }}
</n-tag> </n-tag>
@ -28,6 +31,7 @@ import { useRoute } from 'vue-router';
import { NSpace, NTag } from 'naive-ui'; import { NSpace, NTag } from 'naive-ui';
import { useThemeStore, useAppStore } from '@/store'; import { useThemeStore, useAppStore } from '@/store';
import { useRouterChange } from '@/hooks'; import { useRouterChange } from '@/hooks';
import { ROUTE_HOME } from '@/router';
defineProps({ defineProps({
zIndex: { zIndex: {
@ -39,7 +43,7 @@ defineProps({
const route = useRoute(); const route = useRoute();
const theme = useThemeStore(); const theme = useThemeStore();
const app = useAppStore(); const app = useAppStore();
const { initMultiTab, addMultiTab, setActiveMultiTab, handleClickTab } = useAppStore(); const { initMultiTab, addMultiTab, removeMultiTab, setActiveMultiTab, handleClickTab } = useAppStore();
const { toReload } = useRouterChange(); const { toReload } = useRouterChange();
const fixedHeaderAndTab = computed(() => theme.fixedHeaderAndTab || theme.navStyle.mode === 'horizontal-mix'); const fixedHeaderAndTab = computed(() => theme.fixedHeaderAndTab || theme.navStyle.mode === 'horizontal-mix');

View File

@ -4,19 +4,24 @@
<global-header v-if="isHorizontalMix" :z-index="2" /> <global-header v-if="isHorizontalMix" :z-index="2" />
<div class="flex-1-hidden flex h-full"> <div class="flex-1-hidden flex h-full">
<global-sider v-if="isHorizontalMix" class="sider-margin" :z-index="1" /> <global-sider v-if="isHorizontalMix" class="sider-margin" :z-index="1" />
<n-scrollbar ref="scrollbar" class="h-full" :x-scrollable="true" :content-class="fullPage ? 'h-full' : ''"> <n-scrollbar
ref="scrollbar"
class="h-full"
:x-scrollable="true"
:content-class="routeProps.fullPage ? 'h-full' : ''"
>
<div <div
class="inline-flex-col-stretch w-full" class="inline-flex-col-stretch w-full"
:class="[{ 'content-padding': isHorizontalMix }, fullPage ? 'h-full' : 'min-h-100vh']" :class="[{ 'content-padding': isHorizontalMix }, routeProps.fullPage ? 'h-full' : 'min-h-100vh']"
> >
<global-header v-if="!isHorizontalMix" :z-index="1" /> <global-header v-if="!isHorizontalMix" :z-index="1" />
<global-tab :z-index="1" /> <global-tab :z-index="1" />
<n-layout-content class="flex-auto" :class="{ 'bg-[#f5f7f9]': !theme.darkMode }"> <n-layout-content class="flex-auto" :class="{ 'bg-[#f5f7f9]': !theme.darkMode }">
<router-view v-slot="{ Component }"> <router-view v-slot="{ Component }">
<keep-alive v-if="keepAlive"> <keep-alive>
<component :is="Component" /> <component :is="Component" v-if="routeProps.keepAlive" :key="routeProps.name" />
</keep-alive> </keep-alive>
<component :is="Component" v-else /> <component :is="Component" v-if="!routeProps.keepAlive" :key="routeProps.name" />
</router-view> </router-view>
</n-layout-content> </n-layout-content>
<global-footer /> <global-footer />
@ -29,15 +34,14 @@
<script lang="ts" setup> <script lang="ts" setup>
import { computed } from 'vue'; import { computed } from 'vue';
import { useRoute } from 'vue-router';
import { NLayout, NScrollbar, NLayoutContent } from 'naive-ui'; import { NLayout, NScrollbar, NLayoutContent } from 'naive-ui';
import { useThemeStore } from '@/store'; import { useThemeStore } from '@/store';
import { useScrollBehavior } from '@/hooks';
import { GlobalSider, GlobalHeader, GlobalTab, GlobalFooter, SettingDrawer } from './components'; import { GlobalSider, GlobalHeader, GlobalTab, GlobalFooter, SettingDrawer } from './components';
import { useRouteProps, useScrollBehavior } from '../composables';
const route = useRoute();
const theme = useThemeStore(); const theme = useThemeStore();
const { scrollbar, resetScrollWatcher } = useScrollBehavior(); const { scrollbar } = useScrollBehavior();
const routeProps = useRouteProps();
const isHorizontalMix = computed(() => theme.navStyle.mode === 'horizontal-mix'); const isHorizontalMix = computed(() => theme.navStyle.mode === 'horizontal-mix');
const headerAndMultiTabHeight = computed(() => { const headerAndMultiTabHeight = computed(() => {
@ -47,15 +51,6 @@ const headerAndMultiTabHeight = computed(() => {
} = theme; } = theme;
return `${hHeight + mHeight}px`; return `${hHeight + mHeight}px`;
}); });
/** 缓存页面 */
const keepAlive = computed(() => Boolean(route.meta?.keepAlive));
/** 100%视高 */
const fullPage = computed(() => Boolean(route.meta?.fullPage));
//
resetScrollWatcher();
</script> </script>
<style scoped> <style scoped>
:deep(.n-scrollbar-rail) { :deep(.n-scrollbar-rail) {

View File

@ -1,31 +1,21 @@
<template> <template>
<n-scrollbar ref="scrollbar" class="h-full" :x-scrollable="true" :content-class="fullPage ? 'h-full' : ''"> <n-scrollbar ref="scrollbar" class="h-full" :x-scrollable="true" :content-class="routeProps.fullPage ? 'h-full' : ''">
<div class="inline-block w-full" :class="[fullPage ? 'h-full' : 'min-h-100vh']"> <div class="inline-block w-full" :class="[routeProps.fullPage ? 'h-full' : 'min-h-100vh']">
<router-view v-slot="{ Component }"> <router-view v-slot="{ Component }">
<keep-alive v-if="keepAlive"> <keep-alive>
<component :is="Component" /> <component :is="Component" v-if="routeProps.keepAlive" :key="routeProps.name" />
</keep-alive> </keep-alive>
<component :is="Component" v-else /> <component :is="Component" v-if="!routeProps.keepAlive" :key="routeProps.name" />
</router-view> </router-view>
</div> </div>
</n-scrollbar> </n-scrollbar>
</template> </template>
<script lang="ts" setup> <script lang="ts" setup>
import { useRoute } from 'vue-router';
import { NScrollbar } from 'naive-ui'; import { NScrollbar } from 'naive-ui';
import { computed } from 'vue'; import { useRouteProps, useScrollBehavior } from '../composables';
import { useScrollBehavior } from '@/hooks';
const route = useRoute(); const { scrollbar } = useScrollBehavior();
const { scrollbar, resetScrollWatcher } = useScrollBehavior(); const routeProps = useRouteProps();
/** 缓存页面 */
const keepAlive = computed(() => Boolean(route.meta?.keepAlive));
/** 100%视高 */
const fullPage = computed(() => Boolean(route.meta?.fullPage));
resetScrollWatcher();
</script> </script>
<style scoped></style> <style scoped></style>

View File

@ -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<HTMLElement | null>(null);
const route = useRoute();
function resetScrollBehavior() {
scrollbar.value?.scrollTo({ left: 0, top: 0 });
}
watch(
() => route.name,
() => {
resetScrollBehavior();
}
);
return {
scrollbar
};
}

View File

@ -60,11 +60,11 @@ const appStore = defineStore({
toggleMenu() { toggleMenu() {
this.menu.collapsed = !this.menu.collapsed; this.menu.collapsed = !this.menu.collapsed;
}, },
/** 判断tab路由是否存在某个路由 */ /** 判断页签路由是否存在某个路由 */
getIndexInTabRoutes(fullPath: string) { getIndexInTabRoutes(fullPath: string) {
return this.multiTab.routes.findIndex(item => item.fullPath === fullPath); return this.multiTab.routes.findIndex(item => item.fullPath === fullPath);
}, },
/** 添加多tab的数据 */ /** 添加多页签的数据 */
addMultiTab(route: RouteLocationNormalizedLoaded) { addMultiTab(route: RouteLocationNormalizedLoaded) {
const { fullPath } = route; const { fullPath } = route;
const isExist = this.getIndexInTabRoutes(fullPath) > -1; const isExist = this.getIndexInTabRoutes(fullPath) > -1;
@ -72,13 +72,24 @@ const appStore = defineStore({
this.multiTab.routes.push({ ...route }); 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) { handleClickTab(fullPath: string) {
if (this.multiTab.activeRoute !== fullPath) { if (this.multiTab.activeRoute !== fullPath) {
router.push(fullPath); router.push(fullPath);
this.setActiveMultiTab(fullPath); this.setActiveMultiTab(fullPath);
} }
}, },
/** 设置当前路由对应的tab为激活状态 */ /** 设置当前路由对应的页签为激活状态 */
setActiveMultiTab(fullPath: string) { setActiveMultiTab(fullPath: string) {
this.multiTab.activeRoute = fullPath; this.multiTab.activeRoute = fullPath;
}, },