Commit 6bffdb5c64aa139cf6119b50aeed42629a65f07b
1 parent
c620f827
fix: fix the disappearance of tab switching parameters (#56)
Showing
6 changed files
with
71 additions
and
34 deletions
src/layouts/default/LayoutHeader.tsx
@@ -22,12 +22,13 @@ import { useModal } from '/@/components/Modal/index'; | @@ -22,12 +22,13 @@ import { useModal } from '/@/components/Modal/index'; | ||
22 | import { errorStore } from '/@/store/modules/error'; | 22 | import { errorStore } from '/@/store/modules/error'; |
23 | import { useWindowSizeFn } from '/@/hooks/event/useWindowSize'; | 23 | import { useWindowSizeFn } from '/@/hooks/event/useWindowSize'; |
24 | import NoticeAction from './actions/notice/NoticeActionItem.vue'; | 24 | import NoticeAction from './actions/notice/NoticeActionItem.vue'; |
25 | - | 25 | +import { useRouter } from 'vue-router'; |
26 | export default defineComponent({ | 26 | export default defineComponent({ |
27 | name: 'DefaultLayoutHeader', | 27 | name: 'DefaultLayoutHeader', |
28 | setup() { | 28 | setup() { |
29 | const widthRef = ref(200); | 29 | const widthRef = ref(200); |
30 | - const { refreshPage, addTab } = useTabs(); | 30 | + const { refreshPage } = useTabs(); |
31 | + const { push } = useRouter(); | ||
31 | const [register, { openModal }] = useModal(); | 32 | const [register, { openModal }] = useModal(); |
32 | const { toggleFullscreen, isFullscreenRef } = useFullscreen(); | 33 | const { toggleFullscreen, isFullscreenRef } = useFullscreen(); |
33 | 34 | ||
@@ -70,7 +71,7 @@ export default defineComponent({ | @@ -70,7 +71,7 @@ export default defineComponent({ | ||
70 | 71 | ||
71 | function handleToErrorList() { | 72 | function handleToErrorList() { |
72 | errorStore.commitErrorListCountState(0); | 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,8 +22,8 @@ import { | ||
22 | import { useRouter } from 'vue-router'; | 22 | import { useRouter } from 'vue-router'; |
23 | import { useThrottle } from '/@/hooks/core/useThrottle'; | 23 | import { useThrottle } from '/@/hooks/core/useThrottle'; |
24 | import { permissionStore } from '/@/store/modules/permission'; | 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 | // import | 28 | // import |
29 | export default defineComponent({ | 29 | export default defineComponent({ |
@@ -53,8 +53,8 @@ export default defineComponent({ | @@ -53,8 +53,8 @@ export default defineComponent({ | ||
53 | setup(props) { | 53 | setup(props) { |
54 | const menusRef = ref<Menu[]>([]); | 54 | const menusRef = ref<Menu[]>([]); |
55 | const flatMenusRef = ref<Menu[]>([]); | 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 | const getProjectConfigRef = computed(() => { | 59 | const getProjectConfigRef = computed(() => { |
60 | return appStore.getProjectConfig; | 60 | return appStore.getProjectConfig; |
@@ -144,7 +144,8 @@ export default defineComponent({ | @@ -144,7 +144,8 @@ export default defineComponent({ | ||
144 | if (splitType === MenuSplitTyeEnum.TOP) { | 144 | if (splitType === MenuSplitTyeEnum.TOP) { |
145 | menuStore.commitCurrentTopSplitMenuPathState(path); | 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 '/@/hooks/web/useTabs'; | @@ -27,6 +27,7 @@ import { useTabs } from '/@/hooks/web/useTabs'; | ||
27 | // import { PageEnum } from '/@/enums/pageEnum'; | 27 | // import { PageEnum } from '/@/enums/pageEnum'; |
28 | 28 | ||
29 | import './index.less'; | 29 | import './index.less'; |
30 | +import { userStore } from '/@/store/modules/user'; | ||
30 | export default defineComponent({ | 31 | export default defineComponent({ |
31 | name: 'MultiTabs', | 32 | name: 'MultiTabs', |
32 | setup() { | 33 | setup() { |
@@ -60,24 +61,27 @@ export default defineComponent({ | @@ -60,24 +61,27 @@ export default defineComponent({ | ||
60 | 61 | ||
61 | watch( | 62 | watch( |
62 | () => unref(currentRoute).path, | 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 | // 使用useTab的addTab的话,当用户手动调转,需要自行调用addTab | 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 | immediate: true, | 85 | immediate: true, |
82 | } | 86 | } |
83 | ); | 87 | ); |
@@ -115,7 +119,9 @@ export default defineComponent({ | @@ -115,7 +119,9 @@ export default defineComponent({ | ||
115 | // 关闭当前ab | 119 | // 关闭当前ab |
116 | function handleEdit(targetKey: string) { | 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 | index !== -1 && closeTab(unref(getTabsState)[index]); | 125 | index !== -1 && closeTab(unref(getTabsState)[index]); |
120 | } | 126 | } |
121 | 127 | ||
@@ -133,8 +139,10 @@ export default defineComponent({ | @@ -133,8 +139,10 @@ export default defineComponent({ | ||
133 | } | 139 | } |
134 | function renderTabs() { | 140 | function renderTabs() { |
135 | return unref(getTabsState).map((item: TabItem) => { | 141 | return unref(getTabsState).map((item: TabItem) => { |
142 | + const key = item.query ? item.fullPath : item.path; | ||
143 | + | ||
136 | return ( | 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 | tab: () => <TabContent tabItem={item} />, | 147 | tab: () => <TabContent tabItem={item} />, |
140 | }} | 148 | }} |
src/layouts/default/multitabs/useTabDropdown.ts
@@ -12,6 +12,7 @@ import { PageEnum } from '/@/enums/pageEnum'; | @@ -12,6 +12,7 @@ import { PageEnum } from '/@/enums/pageEnum'; | ||
12 | import { useGo, useRedo } from '/@/hooks/web/usePage'; | 12 | import { useGo, useRedo } from '/@/hooks/web/usePage'; |
13 | import router from '/@/router'; | 13 | import router from '/@/router'; |
14 | import { useTabs, isInitUseTab } from '/@/hooks/web/useTabs'; | 14 | import { useTabs, isInitUseTab } from '/@/hooks/web/useTabs'; |
15 | +import { RouteLocationRaw } from 'vue-router'; | ||
15 | 16 | ||
16 | const { initTabFn } = useTabs(); | 17 | const { initTabFn } = useTabs(); |
17 | /** | 18 | /** |
@@ -92,7 +93,11 @@ export function useTabDropdown(tabContentProps: TabContentProps) { | @@ -92,7 +93,11 @@ export function useTabDropdown(tabContentProps: TabContentProps) { | ||
92 | let toPath: PageEnum | string = PageEnum.BASE_HOME; | 93 | let toPath: PageEnum | string = PageEnum.BASE_HOME; |
93 | 94 | ||
94 | if (len > 0) { | 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 | path !== toPath && go(toPath as PageEnum, true); | 103 | path !== toPath && go(toPath as PageEnum, true); |
@@ -192,7 +197,7 @@ export function useTabDropdown(tabContentProps: TabContentProps) { | @@ -192,7 +197,7 @@ export function useTabDropdown(tabContentProps: TabContentProps) { | ||
192 | } | 197 | } |
193 | return { getDropMenuList, handleMenuEvent }; | 198 | return { getDropMenuList, handleMenuEvent }; |
194 | } | 199 | } |
195 | -export function closeTab(closedTab: TabItem) { | 200 | +export function closeTab(closedTab: TabItem | AppRouteRecordRaw) { |
196 | const { currentRoute, replace } = router; | 201 | const { currentRoute, replace } = router; |
197 | // 当前tab列表 | 202 | // 当前tab列表 |
198 | const getTabsState = computed(() => { | 203 | const getTabsState = computed(() => { |
@@ -205,23 +210,35 @@ export function closeTab(closedTab: TabItem) { | @@ -205,23 +210,35 @@ export function closeTab(closedTab: TabItem) { | ||
205 | return; | 210 | return; |
206 | } | 211 | } |
207 | // 关闭的为激活atb | 212 | // 关闭的为激活atb |
208 | - let toPath: PageEnum | string; | 213 | + let toObj: RouteLocationRaw = {}; |
209 | const index = unref(getTabsState).findIndex((item) => item.path === path); | 214 | const index = unref(getTabsState).findIndex((item) => item.path === path); |
210 | 215 | ||
211 | // 如果当前为最左边tab | 216 | // 如果当前为最左边tab |
212 | if (index === 0) { | 217 | if (index === 0) { |
213 | // 只有一个tab,则跳转至首页,否则跳转至右tab | 218 | // 只有一个tab,则跳转至首页,否则跳转至右tab |
214 | if (unref(getTabsState).length === 1) { | 219 | if (unref(getTabsState).length === 1) { |
215 | - toPath = PageEnum.BASE_HOME; | 220 | + toObj = PageEnum.BASE_HOME; |
216 | } else { | 221 | } else { |
217 | // 跳转至右边tab | 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 | } else { | 231 | } else { |
221 | // 跳转至左边tab | 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 | const route = (unref(currentRoute) as unknown) as AppRouteRecordRaw; | 241 | const route = (unref(currentRoute) as unknown) as AppRouteRecordRaw; |
225 | tabStore.commitCloseTab(route); | 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,11 +98,21 @@ class Tab extends VuexModule { | ||
98 | return; | 98 | return; |
99 | } | 99 | } |
100 | 100 | ||
101 | + let updateIndex = -1; | ||
101 | // 已经存在的页面,不重复添加tab | 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 | this.tabsState.push({ path, fullPath, name, meta, params, query }); | 117 | this.tabsState.push({ path, fullPath, name, meta, params, query }); |
108 | if (unref(getOpenKeepAliveRef) && name) { | 118 | if (unref(getOpenKeepAliveRef) && name) { |
src/views/demo/feat/tab-params/index.vue