Skip to content

Commit

Permalink
fix(components): 页面跳转被拦截, 则会出现 tab 页签与页面不一致的问题
Browse files Browse the repository at this point in the history
  • Loading branch information
taisha committed Feb 8, 2023
1 parent da521b3 commit bd5dd2c
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 13 deletions.
4 changes: 2 additions & 2 deletions src/composables/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ export function useRouterPush(inSetup = true) {
if (newTab) {
const routerData = router.resolve(to);
window.open(routerData.href, '_blank');
} else {
router.push(to);
return Promise.resolve();
}
return router.push(to);
}

/** 返回上一级路由 */
Expand Down
30 changes: 19 additions & 11 deletions src/store/modules/tab/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,34 +114,42 @@ export const useTabStore = defineStore('tab-store', {
* 删除多页签
* @param fullPath - 路由fullPath
*/
removeTab(fullPath: string) {
async removeTab(fullPath: string) {
const { routerPush } = useRouterPush(false);

const isActive = this.activeTab === fullPath;
const updateTabs = this.tabs.filter(tab => tab.fullPath !== fullPath);
this.tabs = updateTabs;
if (!isActive) {
this.tabs = updateTabs;
}
if (isActive && updateTabs.length) {
const activePath = updateTabs[updateTabs.length - 1].fullPath;
this.setActiveTab(activePath);
routerPush(activePath);
const navigationFailure = await routerPush(activePath);
if (!navigationFailure) {
this.tabs = updateTabs;
this.setActiveTab(activePath);
}
}
},
/**
* 清空多页签(多页签首页保留)
* @param excludes - 保留的多页签path
*/
clearTab(excludes: string[] = []) {
async clearTab(excludes: string[] = []) {
const { routerPush } = useRouterPush(false);

const homePath = this.homeTab.fullPath;
const remain = [homePath, ...excludes];
const hasActive = remain.includes(this.activeTab);
const updateTabs = this.tabs.filter(tab => remain.includes(tab.fullPath));
this.tabs = updateTabs;
if (hasActive) this.tabs = updateTabs;
if (!hasActive && updateTabs.length) {
const activePath = updateTabs[updateTabs.length - 1].fullPath;
this.setActiveTab(activePath);
routerPush(activePath);
const navigationFailure = await routerPush(activePath);
if (!navigationFailure) {
this.tabs = updateTabs;
this.setActiveTab(activePath);
}
}
},
/**
Expand Down Expand Up @@ -174,13 +182,13 @@ export const useTabStore = defineStore('tab-store', {
* 点击单个tab
* @param fullPath - 路由fullPath
*/
handleClickTab(fullPath: string) {
async handleClickTab(fullPath: string) {
const { routerPush } = useRouterPush(false);

const isActive = this.activeTab === fullPath;
if (!isActive) {
this.setActiveTab(fullPath);
routerPush(fullPath);
const navigationFailure = await routerPush(fullPath);
if (!navigationFailure) this.setActiveTab(fullPath);
}
},
/**
Expand Down

0 comments on commit bd5dd2c

Please sign in to comment.