fix(components): 页面跳转被拦截, 则会出现 tab 页签与页面不一致的问题

This commit is contained in:
刘璐 2023-02-08 22:29:54 +08:00
parent da521b35e6
commit bd5dd2cf28
2 changed files with 21 additions and 13 deletions

View File

@ -19,9 +19,9 @@ export function useRouterPush(inSetup = true) {
if (newTab) { if (newTab) {
const routerData = router.resolve(to); const routerData = router.resolve(to);
window.open(routerData.href, '_blank'); window.open(routerData.href, '_blank');
} else { return Promise.resolve();
router.push(to);
} }
return router.push(to);
} }
/** 返回上一级路由 */ /** 返回上一级路由 */

View File

@ -114,34 +114,42 @@ export const useTabStore = defineStore('tab-store', {
* *
* @param fullPath - fullPath * @param fullPath - fullPath
*/ */
removeTab(fullPath: string) { async removeTab(fullPath: string) {
const { routerPush } = useRouterPush(false); const { routerPush } = useRouterPush(false);
const isActive = this.activeTab === fullPath; const isActive = this.activeTab === fullPath;
const updateTabs = this.tabs.filter(tab => tab.fullPath !== fullPath); const updateTabs = this.tabs.filter(tab => tab.fullPath !== fullPath);
this.tabs = updateTabs; if (!isActive) {
this.tabs = updateTabs;
}
if (isActive && updateTabs.length) { if (isActive && updateTabs.length) {
const activePath = updateTabs[updateTabs.length - 1].fullPath; const activePath = updateTabs[updateTabs.length - 1].fullPath;
this.setActiveTab(activePath); const navigationFailure = await routerPush(activePath);
routerPush(activePath); if (!navigationFailure) {
this.tabs = updateTabs;
this.setActiveTab(activePath);
}
} }
}, },
/** /**
* () * ()
* @param excludes - path * @param excludes - path
*/ */
clearTab(excludes: string[] = []) { async clearTab(excludes: string[] = []) {
const { routerPush } = useRouterPush(false); const { routerPush } = useRouterPush(false);
const homePath = this.homeTab.fullPath; const homePath = this.homeTab.fullPath;
const remain = [homePath, ...excludes]; const remain = [homePath, ...excludes];
const hasActive = remain.includes(this.activeTab); const hasActive = remain.includes(this.activeTab);
const updateTabs = this.tabs.filter(tab => remain.includes(tab.fullPath)); const updateTabs = this.tabs.filter(tab => remain.includes(tab.fullPath));
this.tabs = updateTabs; if (hasActive) this.tabs = updateTabs;
if (!hasActive && updateTabs.length) { if (!hasActive && updateTabs.length) {
const activePath = updateTabs[updateTabs.length - 1].fullPath; const activePath = updateTabs[updateTabs.length - 1].fullPath;
this.setActiveTab(activePath); const navigationFailure = await routerPush(activePath);
routerPush(activePath); if (!navigationFailure) {
this.tabs = updateTabs;
this.setActiveTab(activePath);
}
} }
}, },
/** /**
@ -174,13 +182,13 @@ export const useTabStore = defineStore('tab-store', {
* tab * tab
* @param fullPath - fullPath * @param fullPath - fullPath
*/ */
handleClickTab(fullPath: string) { async handleClickTab(fullPath: string) {
const { routerPush } = useRouterPush(false); const { routerPush } = useRouterPush(false);
const isActive = this.activeTab === fullPath; const isActive = this.activeTab === fullPath;
if (!isActive) { if (!isActive) {
this.setActiveTab(fullPath); const navigationFailure = await routerPush(fullPath);
routerPush(fullPath); if (!navigationFailure) this.setActiveTab(fullPath);
} }
}, },
/** /**