Skip to content

Commit 59e683c

Browse files
committed
refactor!: 扩展路由 close 函数,并移除 useTabbar 中 close 函数
1 parent 8d59733 commit 59e683c

File tree

2 files changed

+24
-10
lines changed

2 files changed

+24
-10
lines changed

src/router/extensions.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,30 @@ function extendReplace(router: Router) {
5555
}
5656
}
5757

58+
declare module 'vue-router' {
59+
interface Router {
60+
/**
61+
* 本方法为框架扩展语法,等同于 `push` 方法,并且同时会关闭当前标签页
62+
*/
63+
close: (to: RouteLocationRaw) => Promise<NavigationFailure | void | undefined>
64+
}
65+
}
66+
67+
function extendClose(router: Router) {
68+
router.close = function (to: RouteLocationRaw) {
69+
const settingsStore = useSettingsStore(pinia)
70+
if (settingsStore.settings.tabbar.enable) {
71+
const tabId = getId(router)
72+
const tabbarStore = useTabbarStore(pinia)
73+
tabbarStore.remove(tabId)
74+
}
75+
return router.push(to)
76+
}
77+
}
78+
5879
export default function setupExtensions(router: Router) {
5980
extendPush(router)
6081
extendGo(router)
6182
extendReplace(router)
83+
extendClose(router)
6284
}

src/utils/composables/useTabbar.ts

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import type { RouteLocationRaw } from 'vue-router'
21
import { toast } from 'vue-sonner'
32

43
export default function useTabbar() {
@@ -11,24 +10,17 @@ export default function useTabbar() {
1110
return route.fullPath
1211
}
1312

14-
function close(to: RouteLocationRaw) {
15-
const tabId = getId()
16-
router.push(to).then(() => {
17-
tabbarStore.remove(tabId)
18-
})
19-
}
20-
2113
function closeById(tabId = getId()) {
2214
if (checkClose(tabId, false)) {
2315
const activedTabId = getId()
2416
// 如果关闭的标签正好是当前路由
2517
if (tabId === activedTabId) {
2618
const index = tabbarStore.list.findIndex(item => item.tabId === tabId)
2719
if (index > 0) {
28-
close(tabbarStore.list[index - 1].fullPath)
20+
router.close(tabbarStore.list[index - 1].fullPath)
2921
}
3022
else {
31-
close(tabbarStore.list[index + 1].fullPath)
23+
router.close(tabbarStore.list[index + 1].fullPath)
3224
}
3325
}
3426
else {

0 commit comments

Comments
 (0)