Commit e85649bde2378e8118099879392ff024da33e12b

Authored by 无木
1 parent cecdfbaf

fix(tabs): close tab without navigation

修复多标签在某些情况下关闭页签不会跳转路由的问题

fixed: #1131
CHANGELOG.zh_CN.md
... ... @@ -7,6 +7,7 @@
7 7 - 修复点击遮罩、按下`Esc`键都不能关闭`Modal`的问题
8 8 - 修复点击关闭按钮、最大化按钮旁边的空白区域也会导致`Modal`关闭的问题
9 9 - **BasicTree** 修复节点插槽不起作用的问题
  10 +- **其它** 修复多标签在某些情况下关闭页签不会跳转路由的问题
10 11  
11 12 ## 2.7.2(2021-09-14)
12 13  
... ...
src/store/modules/multipleTab.ts
... ... @@ -26,6 +26,15 @@ function handleGotoPage(router: Router) {
26 26 go(unref(router.currentRoute).path, true);
27 27 }
28 28  
  29 +const getToTarget = (tabItem: RouteLocationNormalized) => {
  30 + const { params, path, query } = tabItem;
  31 + return {
  32 + params: params || {},
  33 + path,
  34 + query: query || {},
  35 + };
  36 +};
  37 +
29 38 const cacheTab = projectSetting.multiTabsSetting.cache;
30 39  
31 40 export const useMultipleTabStore = defineStore({
... ... @@ -147,15 +156,6 @@ export const useMultipleTabStore = defineStore({
147 156 },
148 157  
149 158 async closeTab(tab: RouteLocationNormalized, router: Router) {
150   - const getToTarget = (tabItem: RouteLocationNormalized) => {
151   - const { params, path, query } = tabItem;
152   - return {
153   - params: params || {},
154   - path,
155   - query: query || {},
156   - };
157   - };
158   -
159 159 const close = (route: RouteLocationNormalized) => {
160 160 const { fullPath, meta: { affix } = {} } = route;
161 161 if (affix) {
... ... @@ -196,13 +196,36 @@ export const useMultipleTabStore = defineStore({
196 196 toTarget = getToTarget(page);
197 197 }
198 198 close(currentRoute.value);
199   - replace(toTarget);
  199 + await replace(toTarget);
200 200 },
201 201  
202 202 // Close according to key
203 203 async closeTabByKey(key: string, router: Router) {
204 204 const index = this.tabList.findIndex((item) => (item.fullPath || item.path) === key);
205   - index !== -1 && this.closeTab(this.tabList[index], router);
  205 + if (index !== -1) {
  206 + await this.closeTab(this.tabList[index], router);
  207 + const { currentRoute, replace } = router;
  208 + // 检查当前路由是否存在于tabList中
  209 + const isActivated = this.tabList.findIndex((item) => {
  210 + return item.fullPath === currentRoute.value.fullPath;
  211 + });
  212 + // 如果当前路由不存在于TabList中,尝试切换到其它路由
  213 + if (isActivated === -1) {
  214 + let pageIndex;
  215 + if (index > 0) {
  216 + pageIndex = index - 1;
  217 + } else if (index < this.tabList.length - 1) {
  218 + pageIndex = index + 1;
  219 + } else {
  220 + pageIndex = -1;
  221 + }
  222 + if (pageIndex >= 0) {
  223 + const page = this.tabList[index - 1];
  224 + const toTarget = getToTarget(page);
  225 + await replace(toTarget);
  226 + }
  227 + }
  228 + }
206 229 },
207 230  
208 231 // Sort the tabs
... ...