扩展路由 close 函数,并移除 useTabbar 中 close 函数

This commit is contained in:
Hooray
2025-06-21 15:19:47 +08:00
parent 97b15818b5
commit e61465f75e
3 changed files with 25 additions and 11 deletions
+22
View File
@@ -55,8 +55,30 @@ function extendReplace(router: Router) {
}
}
declare module 'vue-router' {
interface Router {
/**
* 本方法为框架扩展语法,等同于 `push` 方法,并且同时会关闭当前标签页
*/
close: (to: RouteLocationRaw) => Promise<NavigationFailure | void | undefined>
}
}
function extendClose(router: Router) {
router.close = function (to: RouteLocationRaw) {
const settingsStore = useSettingsStore(pinia)
if (settingsStore.settings.tabbar.enable) {
const tabId = getId(router)
const tabbarStore = useTabbarStore(pinia)
tabbarStore.remove(tabId)
}
return router.push(to)
}
}
export default function setupExtensions(router: Router) {
extendPush(router)
extendGo(router)
extendReplace(router)
extendClose(router)
}
+2 -10
View File
@@ -1,4 +1,3 @@
import type { RouteLocationRaw } from 'vue-router'
import { toast } from 'vue-sonner'
export default function useTabbar() {
@@ -11,13 +10,6 @@ export default function useTabbar() {
return route.fullPath
}
function close(to: RouteLocationRaw) {
const tabId = getId()
router.push(to).then(() => {
tabbarStore.remove(tabId)
})
}
function closeById(tabId = getId()) {
if (checkClose(tabId, false)) {
const activedTabId = getId()
@@ -25,10 +17,10 @@ export default function useTabbar() {
if (tabId === activedTabId) {
const index = tabbarStore.list.findIndex(item => item.tabId === tabId)
if (index > 0) {
close(tabbarStore.list[index - 1].fullPath)
router.close(tabbarStore.list[index - 1].fullPath)
}
else {
close(tabbarStore.list[index + 1].fullPath)
router.close(tabbarStore.list[index + 1].fullPath)
}
}
else {
+1 -1
View File
@@ -12,7 +12,7 @@ function open() {
router.push(settingsStore.settings.home.fullPath)
}
function close() {
tabbar.close(settingsStore.settings.home.fullPath)
router.close(settingsStore.settings.home.fullPath)
}
function closeById() {
tabbar.closeById()