File tree Expand file tree Collapse file tree 3 files changed +25
-11
lines changed
Expand file tree Collapse file tree 3 files changed +25
-11
lines changed Original file line number Diff line number Diff 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 < void >
64+ }
65+ }
66+
67+ function extendClose ( router : Router ) {
68+ router . close = async function ( to : RouteLocationRaw ) {
69+ await router . push ( to )
70+ const settingsStore = useSettingsStore ( pinia )
71+ if ( settingsStore . settings . tabbar . enable ) {
72+ const tabId = getId ( router )
73+ const tabbarStore = useTabbarStore ( pinia )
74+ tabbarStore . remove ( tabId )
75+ }
76+ }
77+ }
78+
5879export default function setupExtensions ( router : Router ) {
5980 extendPush ( router )
6081 extendGo ( router )
6182 extendReplace ( router )
83+ extendClose ( router )
6284}
Original file line number Diff line number Diff line change 1- import type { RouteLocationRaw } from 'vue-router'
21import { toast } from 'vue-sonner'
32
43export 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 {
Original file line number Diff line number Diff line change @@ -12,7 +12,7 @@ function open() {
1212 router .push (settingsStore .settings .home .fullPath )
1313}
1414function close() {
15- tabbar .close (settingsStore .settings .home .fullPath )
15+ router .close (settingsStore .settings .home .fullPath )
1616}
1717function closeById() {
1818 tabbar .closeById ()
You can’t perform that action at this time.
0 commit comments