refactor!: 重写路由 push/go/replace 函数,移除 useTabbar 中 open/go/replace 函数

This commit is contained in:
Hooray
2025-03-29 01:53:13 +08:00
parent 10c8401a88
commit 2b18c3f138
3 changed files with 66 additions and 25 deletions
+64
View File
@@ -0,0 +1,64 @@
import type { RouteLocationRaw, Router } from 'vue-router'
import pinia from '@/store'
import useSettingsStore from '@/store/modules/settings'
import useTabbarStore from '@/store/modules/tabbar'
function getId(router: Router) {
return router.currentRoute.value.fullPath
}
function extendPush(router: Router) {
const originalPush = router.push
router.push = function (to: RouteLocationRaw) {
const settingsStore = useSettingsStore(pinia)
if (settingsStore.settings.tabbar.enable) {
const tabbarStore = useTabbarStore(pinia)
const index = tabbarStore.list.findIndex(item => item.tabId === getId(router))
tabbarStore.$patch({
leaveIndex: index,
})
}
return originalPush(to)
}
}
function extendGo(router: Router) {
const originalGo = router.go
router.go = function (delta: number) {
const settingsStore = useSettingsStore(pinia)
if (settingsStore.settings.tabbar.enable) {
const tabId = getId(router)
const tabbarStore = useTabbarStore(pinia)
originalGo(delta)
if (delta < 0) {
tabbarStore.remove(tabId)
}
}
else {
originalGo(delta)
}
}
}
function extendReplace(router: Router) {
const originalReplace = router.replace
router.replace = function (to: RouteLocationRaw) {
const settingsStore = useSettingsStore(pinia)
if (settingsStore.settings.tabbar.enable) {
const tabId = getId(router)
const tabbarStore = useTabbarStore(pinia)
return originalReplace(to).then(() => {
tabbarStore.remove(tabId)
})
}
else {
return originalReplace(to)
}
}
}
export default function setupExtensions(router: Router) {
extendPush(router)
extendGo(router)
extendReplace(router)
}
+2 -1
View File
@@ -1,8 +1,8 @@
import pinia from '@/store'
import useSettingsStore from '@/store/modules/settings'
import { loadingFadeOut } from 'virtual:app-loading'
import { createRouter, createWebHashHistory } from 'vue-router'
import setupExtensions from './extensions'
import setupGuards from './guards'
// 路由相关数据
import { constantRoutes, constantRoutesByFilesystem } from './routes'
@@ -13,6 +13,7 @@ const router = createRouter({
})
setupGuards(router)
setupExtensions(router)
router.isReady().then(() => {
loadingFadeOut()
-24
View File
@@ -12,27 +12,6 @@ export default function useTabbar() {
return route.fullPath
}
function open(to: RouteLocationRaw) {
const index = tabbarStore.list.findIndex(item => item.tabId === getId())
tabbarStore.$patch({
leaveIndex: index,
})
router.push(to)
}
function go(delta: number) {
const tabId = getId()
router.go(delta)
tabbarStore.remove(tabId)
}
function replace(to: RouteLocationRaw) {
const tabId = getId()
router.replace(to).then(() => {
tabbarStore.remove(tabId)
})
}
function close(to: RouteLocationRaw) {
const tabId = getId()
router.push(to).then(() => {
@@ -170,9 +149,6 @@ export default function useTabbar() {
return {
getId,
open,
go,
replace,
close,
closeById,
closeOtherSide,