Commit 6bffdb5c64aa139cf6119b50aeed42629a65f07b

Authored by vben
1 parent c620f827

fix: fix the disappearance of tab switching parameters (#56)

src/layouts/default/LayoutHeader.tsx
... ... @@ -22,12 +22,13 @@ import { useModal } from '/@/components/Modal/index';
22 22 import { errorStore } from '/@/store/modules/error';
23 23 import { useWindowSizeFn } from '/@/hooks/event/useWindowSize';
24 24 import NoticeAction from './actions/notice/NoticeActionItem.vue';
25   -
  25 +import { useRouter } from 'vue-router';
26 26 export default defineComponent({
27 27 name: 'DefaultLayoutHeader',
28 28 setup() {
29 29 const widthRef = ref(200);
30   - const { refreshPage, addTab } = useTabs();
  30 + const { refreshPage } = useTabs();
  31 + const { push } = useRouter();
31 32 const [register, { openModal }] = useModal();
32 33 const { toggleFullscreen, isFullscreenRef } = useFullscreen();
33 34  
... ... @@ -70,7 +71,7 @@ export default defineComponent({
70 71  
71 72 function handleToErrorList() {
72 73 errorStore.commitErrorListCountState(0);
73   - addTab('/exception/error-log', true);
  74 + push('/exception/error-log');
74 75 }
75 76  
76 77 /**
... ...
src/layouts/default/LayoutMenu.tsx
... ... @@ -22,8 +22,8 @@ import {
22 22 import { useRouter } from 'vue-router';
23 23 import { useThrottle } from '/@/hooks/core/useThrottle';
24 24 import { permissionStore } from '/@/store/modules/permission';
25   -import { useTabs } from '/@/hooks/web/useTabs';
26   -import { PageEnum } from '/@/enums/pageEnum';
  25 +// import { useTabs } from '/@/hooks/web/useTabs';
  26 +// import { PageEnum } from '/@/enums/pageEnum';
27 27  
28 28 // import
29 29 export default defineComponent({
... ... @@ -53,8 +53,8 @@ export default defineComponent({
53 53 setup(props) {
54 54 const menusRef = ref<Menu[]>([]);
55 55 const flatMenusRef = ref<Menu[]>([]);
56   - const { currentRoute } = useRouter();
57   - const { addTab } = useTabs();
  56 + const { currentRoute, push } = useRouter();
  57 + // const { addTab } = useTabs();
58 58  
59 59 const getProjectConfigRef = computed(() => {
60 60 return appStore.getProjectConfig;
... ... @@ -144,7 +144,8 @@ export default defineComponent({
144 144 if (splitType === MenuSplitTyeEnum.TOP) {
145 145 menuStore.commitCurrentTopSplitMenuPathState(path);
146 146 }
147   - addTab(path as PageEnum, true);
  147 + push(path);
  148 + // addTab(path as PageEnum, true);
148 149 }
149 150 }
150 151  
... ...
src/layouts/default/multitabs/index.tsx
... ... @@ -27,6 +27,7 @@ import { useTabs } from &#39;/@/hooks/web/useTabs&#39;;
27 27 // import { PageEnum } from '/@/enums/pageEnum';
28 28  
29 29 import './index.less';
  30 +import { userStore } from '/@/store/modules/user';
30 31 export default defineComponent({
31 32 name: 'MultiTabs',
32 33 setup() {
... ... @@ -60,24 +61,27 @@ export default defineComponent({
60 61  
61 62 watch(
62 63 () => unref(currentRoute).path,
63   - (path) => {
64   - if (activeKeyRef.value !== path) {
65   - activeKeyRef.value = path;
  64 + () => {
  65 + if (!userStore.getTokenState) return;
  66 + const { path: rPath, fullPath } = unref(currentRoute);
  67 + if (activeKeyRef.value !== (fullPath || rPath)) {
  68 + activeKeyRef.value = fullPath || rPath;
66 69 }
67 70 // 监听路由的话虽然可以,但是路由切换的时间会造成卡顿现象?
68 71 // 使用useTab的addTab的话,当用户手动调转,需要自行调用addTab
69   - // tabStore.commitAddTab((unref(currentRoute) as unknown) as AppRouteRecordRaw);
70   - const { affix } = currentRoute.value.meta || {};
71   - if (affix) return;
72   - const hasInTab = tabStore.getTabsState.some(
73   - (item) => item.fullPath === currentRoute.value.fullPath
74   - );
75   - if (!hasInTab) {
76   - tabStore.commitAddTab((unref(currentRoute) as unknown) as AppRouteRecordRaw);
77   - }
  72 + tabStore.commitAddTab((unref(currentRoute) as unknown) as AppRouteRecordRaw);
  73 +
  74 + // const { affix } = currentRoute.value.meta || {};
  75 + // if (affix) return;
  76 + // const hasInTab = tabStore.getTabsState.some(
  77 + // (item) => item.fullPath === currentRoute.value.fullPath
  78 + // );
  79 + // if (!hasInTab) {
  80 + // tabStore.commitAddTab((unref(currentRoute) as unknown) as AppRouteRecordRaw);
  81 + // }
78 82 },
79 83 {
80   - flush: 'post',
  84 + // flush: 'post',
81 85 immediate: true,
82 86 }
83 87 );
... ... @@ -115,7 +119,9 @@ export default defineComponent({
115 119 // 关闭当前ab
116 120 function handleEdit(targetKey: string) {
117 121 // 新增操作隐藏,目前只使用删除操作
118   - const index = unref(getTabsState).findIndex((item) => item.path === targetKey);
  122 + const index = unref(getTabsState).findIndex(
  123 + (item) => (item.fullPath || item.path) === targetKey
  124 + );
119 125 index !== -1 && closeTab(unref(getTabsState)[index]);
120 126 }
121 127  
... ... @@ -133,8 +139,10 @@ export default defineComponent({
133 139 }
134 140 function renderTabs() {
135 141 return unref(getTabsState).map((item: TabItem) => {
  142 + const key = item.query ? item.fullPath : item.path;
  143 +
136 144 return (
137   - <Tabs.TabPane key={item.path} closable={!(item && item.meta && item.meta.affix)}>
  145 + <Tabs.TabPane key={key} closable={!(item && item.meta && item.meta.affix)}>
138 146 {{
139 147 tab: () => <TabContent tabItem={item} />,
140 148 }}
... ...
src/layouts/default/multitabs/useTabDropdown.ts
... ... @@ -12,6 +12,7 @@ import { PageEnum } from &#39;/@/enums/pageEnum&#39;;
12 12 import { useGo, useRedo } from '/@/hooks/web/usePage';
13 13 import router from '/@/router';
14 14 import { useTabs, isInitUseTab } from '/@/hooks/web/useTabs';
  15 +import { RouteLocationRaw } from 'vue-router';
15 16  
16 17 const { initTabFn } = useTabs();
17 18 /**
... ... @@ -92,7 +93,11 @@ export function useTabDropdown(tabContentProps: TabContentProps) {
92 93 let toPath: PageEnum | string = PageEnum.BASE_HOME;
93 94  
94 95 if (len > 0) {
95   - toPath = unref(getTabsState)[len - 1].path;
  96 + const page = unref(getTabsState)[len - 1];
  97 + const p = page.fullPath || page.path;
  98 + if (p) {
  99 + toPath = p;
  100 + }
96 101 }
97 102 // 跳到当前页面报错
98 103 path !== toPath && go(toPath as PageEnum, true);
... ... @@ -192,7 +197,7 @@ export function useTabDropdown(tabContentProps: TabContentProps) {
192 197 }
193 198 return { getDropMenuList, handleMenuEvent };
194 199 }
195   -export function closeTab(closedTab: TabItem) {
  200 +export function closeTab(closedTab: TabItem | AppRouteRecordRaw) {
196 201 const { currentRoute, replace } = router;
197 202 // 当前tab列表
198 203 const getTabsState = computed(() => {
... ... @@ -205,23 +210,35 @@ export function closeTab(closedTab: TabItem) {
205 210 return;
206 211 }
207 212 // 关闭的为激活atb
208   - let toPath: PageEnum | string;
  213 + let toObj: RouteLocationRaw = {};
209 214 const index = unref(getTabsState).findIndex((item) => item.path === path);
210 215  
211 216 // 如果当前为最左边tab
212 217 if (index === 0) {
213 218 // 只有一个tab,则跳转至首页,否则跳转至右tab
214 219 if (unref(getTabsState).length === 1) {
215   - toPath = PageEnum.BASE_HOME;
  220 + toObj = PageEnum.BASE_HOME;
216 221 } else {
217 222 // 跳转至右边tab
218   - toPath = unref(getTabsState)[index + 1].path;
  223 + const page = unref(getTabsState)[index + 1];
  224 + const { params, path, query } = page;
  225 + toObj = {
  226 + params,
  227 + path,
  228 + query,
  229 + };
219 230 }
220 231 } else {
221 232 // 跳转至左边tab
222   - toPath = unref(getTabsState)[index - 1].path;
  233 + const page = unref(getTabsState)[index - 1];
  234 + const { params, path, query } = page;
  235 + toObj = {
  236 + params,
  237 + path,
  238 + query,
  239 + };
223 240 }
224 241 const route = (unref(currentRoute) as unknown) as AppRouteRecordRaw;
225 242 tabStore.commitCloseTab(route);
226   - replace(toPath);
  243 + replace(toObj);
227 244 }
... ...
src/store/modules/tab.ts
... ... @@ -98,11 +98,21 @@ class Tab extends VuexModule {
98 98 return;
99 99 }
100 100  
  101 + let updateIndex = -1;
101 102 // 已经存在的页面,不重复添加tab
102   - const hasTab = this.tabsState.some((tab) => {
103   - return tab.fullPath === fullPath;
  103 + const hasTab = this.tabsState.some((tab, index) => {
  104 + updateIndex = index;
  105 + return (tab.fullPath || tab.path) === (fullPath || path);
104 106 });
105   - if (hasTab) return;
  107 + if (hasTab) {
  108 + const curTab = toRaw(this.tabsState)[updateIndex];
  109 + if (!curTab) return;
  110 + curTab.params = params || curTab.params;
  111 + curTab.query = query || curTab.query;
  112 + curTab.fullPath = fullPath || curTab.fullPath;
  113 + this.tabsState.splice(updateIndex, 1, curTab);
  114 + return;
  115 + }
106 116  
107 117 this.tabsState.push({ path, fullPath, name, meta, params, query });
108 118 if (unref(getOpenKeepAliveRef) && name) {
... ...
src/views/demo/feat/tab-params/index.vue
1 1 <template>
2 2 <div class="p-4">
3 3 Current Param : {{ params }}
4   - <input />
  4 + <!-- <input /> -->
5 5 </div>
6 6 </template>
7 7 <script lang="ts">
... ...