Skip to content

Commit 2b18c3f

Browse files
committed
refactor!: 重写路由 push/go/replace 函数,移除 useTabbar 中 open/go/replace 函数
1 parent 10c8401 commit 2b18c3f

File tree

3 files changed

+66
-25
lines changed

3 files changed

+66
-25
lines changed

‎src/router/extensions.ts‎

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import type { RouteLocationRaw, Router } from 'vue-router'
2+
import pinia from '@/store'
3+
import useSettingsStore from '@/store/modules/settings'
4+
import useTabbarStore from '@/store/modules/tabbar'
5+
6+
function getId(router: Router) {
7+
return router.currentRoute.value.fullPath
8+
}
9+
10+
function extendPush(router: Router) {
11+
const originalPush = router.push
12+
router.push = function (to: RouteLocationRaw) {
13+
const settingsStore = useSettingsStore(pinia)
14+
if (settingsStore.settings.tabbar.enable) {
15+
const tabbarStore = useTabbarStore(pinia)
16+
const index = tabbarStore.list.findIndex(item => item.tabId === getId(router))
17+
tabbarStore.$patch({
18+
leaveIndex: index,
19+
})
20+
}
21+
return originalPush(to)
22+
}
23+
}
24+
25+
function extendGo(router: Router) {
26+
const originalGo = router.go
27+
router.go = function (delta: number) {
28+
const settingsStore = useSettingsStore(pinia)
29+
if (settingsStore.settings.tabbar.enable) {
30+
const tabId = getId(router)
31+
const tabbarStore = useTabbarStore(pinia)
32+
originalGo(delta)
33+
if (delta < 0) {
34+
tabbarStore.remove(tabId)
35+
}
36+
}
37+
else {
38+
originalGo(delta)
39+
}
40+
}
41+
}
42+
43+
function extendReplace(router: Router) {
44+
const originalReplace = router.replace
45+
router.replace = function (to: RouteLocationRaw) {
46+
const settingsStore = useSettingsStore(pinia)
47+
if (settingsStore.settings.tabbar.enable) {
48+
const tabId = getId(router)
49+
const tabbarStore = useTabbarStore(pinia)
50+
return originalReplace(to).then(() => {
51+
tabbarStore.remove(tabId)
52+
})
53+
}
54+
else {
55+
return originalReplace(to)
56+
}
57+
}
58+
}
59+
60+
export default function setupExtensions(router: Router) {
61+
extendPush(router)
62+
extendGo(router)
63+
extendReplace(router)
64+
}

‎src/router/index.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import pinia from '@/store'
22
import useSettingsStore from '@/store/modules/settings'
33
import { loadingFadeOut } from 'virtual:app-loading'
4-
54
import { createRouter, createWebHashHistory } from 'vue-router'
5+
import setupExtensions from './extensions'
66
import setupGuards from './guards'
77
// 路由相关数据
88
import { constantRoutes, constantRoutesByFilesystem } from './routes'
@@ -13,6 +13,7 @@ const router = createRouter({
1313
})
1414

1515
setupGuards(router)
16+
setupExtensions(router)
1617

1718
router.isReady().then(() => {
1819
loadingFadeOut()

‎src/utils/composables/useTabbar.ts‎

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -12,27 +12,6 @@ export default function useTabbar() {
1212
return route.fullPath
1313
}
1414

15-
function open(to: RouteLocationRaw) {
16-
const index = tabbarStore.list.findIndex(item => item.tabId === getId())
17-
tabbarStore.$patch({
18-
leaveIndex: index,
19-
})
20-
router.push(to)
21-
}
22-
23-
function go(delta: number) {
24-
const tabId = getId()
25-
router.go(delta)
26-
tabbarStore.remove(tabId)
27-
}
28-
29-
function replace(to: RouteLocationRaw) {
30-
const tabId = getId()
31-
router.replace(to).then(() => {
32-
tabbarStore.remove(tabId)
33-
})
34-
}
35-
3615
function close(to: RouteLocationRaw) {
3716
const tabId = getId()
3817
router.push(to).then(() => {
@@ -170,9 +149,6 @@ export default function useTabbar() {
170149

171150
return {
172151
getId,
173-
open,
174-
go,
175-
replace,
176152
close,
177153
closeById,
178154
closeOtherSide,

0 commit comments

Comments
 (0)