@@ -11,6 +11,7 @@ import { PageEnum } from '@/enums/pageEnum'
1111
1212interface TabItem {
1313 name : RouteRecordName
14+ fullPath : string
1415 path : string
1516 title ?: string
1617 query ?: LocationQuery
@@ -24,8 +25,8 @@ interface TabsSate {
2425 indexRouteName : RouteRecordName
2526}
2627
27- const getHasTabIndex = ( path : string , tabList : TabItem [ ] ) => {
28- return tabList . findIndex ( ( item ) => item . path == path )
28+ const getHasTabIndex = ( fullPath : string , tabList : TabItem [ ] ) => {
29+ return tabList . findIndex ( ( item ) => item . fullPath == fullPath )
2930}
3031
3132const isCannotAddRoute = ( route : RouteLocationNormalized , router : Router ) => {
@@ -39,8 +40,12 @@ const isCannotAddRoute = (route: RouteLocationNormalized, router: Router) => {
3940 return false
4041}
4142
42- const findTabsIndex = ( path : string , tabList : TabItem [ ] ) => {
43- return tabList . findIndex ( ( item ) => item . path === path )
43+ const findTabsIndex = ( fullPath : string , tabList : TabItem [ ] ) => {
44+ return tabList . findIndex ( ( item ) => item . fullPath === fullPath )
45+ }
46+
47+ const getComponentName = ( route : RouteLocationNormalized ) => {
48+ return route . matched . at ( - 1 ) ?. components ?. default ?. name
4449}
4550
4651export const getRouteParams = ( tabItem : TabItem ) => {
@@ -63,45 +68,67 @@ const useTabsStore = defineStore({
6368 getters : {
6469 getTabList ( ) : TabItem [ ] {
6570 return this . tabList
71+ } ,
72+ getCacheTabList ( ) : string [ ] {
73+ return Array . from ( this . cacheTabList )
6674 }
6775 } ,
6876 actions : {
6977 setRouteName ( name : RouteRecordName ) {
7078 this . indexRouteName = name
7179 } ,
80+ addCache ( componentName ?: string ) {
81+ if ( componentName ) this . cacheTabList . add ( componentName )
82+ } ,
83+ removeCache ( componentName ?: string ) {
84+ if ( componentName && this . cacheTabList . has ( componentName ) ) {
85+ this . cacheTabList . delete ( componentName )
86+ }
87+ console . log ( this . cacheTabList )
88+ } ,
89+ clearCache ( ) {
90+ this . cacheTabList . clear ( )
91+ } ,
7292 resetState ( ) {
7393 this . cacheTabList = new Set ( )
7494 this . tabList = [ ]
7595 this . tasMap = { }
7696 this . indexRouteName = ''
7797 } ,
78- addTab ( route : RouteLocationNormalized , router : Router ) {
79- const { name, path, query, meta, params } = route
98+ addTab ( router : Router ) {
99+ const route = unref ( router . currentRoute )
100+ const { name, query, meta, params, fullPath, path } = route
80101 if ( isCannotAddRoute ( route , router ) ) return
81- const hasTabIndex = getHasTabIndex ( path ! , this . tabList )
82-
102+ const hasTabIndex = getHasTabIndex ( fullPath ! , this . tabList )
103+ const componentName = getComponentName ( route )
83104 const tabItem = {
84105 name : name ! ,
85106 path,
107+ fullPath,
86108 title : meta ?. title ,
87109 query,
88110 params
89111 }
90- this . tasMap [ path ] = tabItem
112+ this . tasMap [ fullPath ] = tabItem
113+ if ( meta ?. keepAlive ) {
114+ this . addCache ( componentName )
115+ }
91116 if ( hasTabIndex != - 1 ) {
92- this . tabList . splice ( hasTabIndex , 1 , tabItem )
93117 return
94118 }
119+
95120 this . tabList . push ( tabItem )
96121 } ,
97- removeTab ( path : string , router : Router ) {
122+ removeTab ( fullPath : string , router : Router ) {
98123 const { currentRoute, push } = router
99- const index = findTabsIndex ( path , this . tabList )
124+ const index = findTabsIndex ( fullPath , this . tabList )
100125 // 移除tab
101126 if ( this . tabList . length > 1 ) {
102127 index !== - 1 && this . tabList . splice ( index , 1 )
103128 }
104- if ( path !== currentRoute . value . path ) {
129+ const componentName = getComponentName ( currentRoute . value )
130+ this . removeCache ( componentName )
131+ if ( fullPath !== currentRoute . value . fullPath ) {
105132 return
106133 }
107134 // 删除选中的tab
@@ -116,17 +143,24 @@ const useTabsStore = defineStore({
116143 const toRoute = getRouteParams ( toTab )
117144 push ( toRoute )
118145 } ,
119- removeOtherTab ( path : string ) {
120- this . tabList = this . tabList . filter ( ( item ) => item . path == path )
146+ removeOtherTab ( route : RouteLocationNormalized ) {
147+ this . tabList = this . tabList . filter ( ( item ) => item . fullPath == route . fullPath )
148+ const componentName = getComponentName ( route )
149+ this . cacheTabList . forEach ( ( name ) => {
150+ if ( componentName !== name ) {
151+ this . removeCache ( name )
152+ }
153+ } )
121154 } ,
122155 removeAllTab ( router : Router ) {
123156 const { push, currentRoute } = router
124- const { path , name } = unref ( currentRoute )
157+ const { name } = unref ( currentRoute )
125158 if ( name == this . indexRouteName ) {
126- this . removeOtherTab ( path )
159+ this . removeOtherTab ( currentRoute . value )
127160 return
128161 }
129162 this . tabList = [ ]
163+ this . clearCache ( )
130164 push ( PageEnum . INDEX )
131165 }
132166 }
0 commit comments