Skip to content

Commit 65266c3

Browse files
authored
Merge pull request #111 from Tencent/optimize/tab-router
feat: 多标签页支持持久化
2 parents d01b7b3 + 9256aec commit 65266c3

3 files changed

Lines changed: 24 additions & 4 deletions

File tree

src/layouts/index.tsx

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { defineComponent, computed, nextTick, onMounted, watch } from 'vue';
1+
import { defineComponent, computed, nextTick, onMounted, watch, onBeforeUnmount } from 'vue';
22
import { storeToRefs } from 'pinia';
33
import { useRoute, useRouter } from 'vue-router';
44
import { usePermissionStore, useSettingStore, useTabsRouterStore } from '@/store';
@@ -37,7 +37,6 @@ export default defineComponent({
3737
const headerMenu = computed(() => {
3838
if (settingStore.layout === 'mix') {
3939
if (settingStore.splitMenu) {
40-
console.log(menuRouters);
4140
return menuRouters.value.map((menu) => ({
4241
...menu,
4342
children: [],
@@ -70,10 +69,28 @@ export default defineComponent({
7069
tabsRouterStore.appendTabRouterList({ path, title: title as string, name, isAlive: true });
7170
};
7271

72+
const getTabRouterListCache = () => {
73+
tabsRouterStore.initTabRouterList(JSON.parse(localStorage.getItem('tabRouterList')));
74+
};
75+
const setTabRouterListCache = () => {
76+
const { tabRouters } = tabsRouterStore;
77+
localStorage.setItem('tabRouterList', JSON.stringify(tabRouters));
78+
};
79+
7380
onMounted(() => {
7481
appendNewRoute();
7582
});
7683

84+
// 如果不需要持久化标签页可以注释掉以下的 onMounted 和 onBeforeUnmount 的内容
85+
onMounted(() => {
86+
if (localStorage.getItem('tabRouterList')) getTabRouterListCache();
87+
window.addEventListener('beforeunload', setTabRouterListCache);
88+
});
89+
90+
onBeforeUnmount(() => {
91+
window.removeEventListener('beforeunload', setTabRouterListCache);
92+
});
93+
7794
watch(
7895
() => route.path,
7996
() => {
@@ -159,7 +176,7 @@ export default defineComponent({
159176
class={`${prefix}-layout-tabs-nav`}
160177
value={route.path}
161178
onChange={handleChangeCurrentTab}
162-
style={{ maxWidth: '100%', position: 'fixed', overflow: 'visible' }}
179+
style={{ width: '100%', position: 'sticky', top: 0 }}
163180
onRemove={handleRemove}
164181
>
165182
{tabRouters.map((router: TRouterInfo, idx: number) => (

src/store/modules/tabs-router.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@ export const useTabsRouterStore = defineStore('tabsRouter', {
4343
removeTabRouterList() {
4444
this.tabRouterList = [];
4545
},
46+
initTabRouterList(newRoutes: TRouterInfo[]) {
47+
this.tabRouterList = newRoutes;
48+
},
4649
},
4750
});
4851

src/style/layout.less

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@
7070
z-index: 999;
7171
}
7272
&-tabs-nav + .@{prefix}-content-layout {
73-
padding-top: 48px + @spacer-3;
73+
padding-top: @spacer-3;
7474
}
7575
}
7676

0 commit comments

Comments
 (0)