11import * as vantIcons from '@react-vant/icons' ;
22import { ArrowLeft } from '@react-vant/icons' ;
3- import { createElement , useRef } from 'react' ;
3+ import { KeepAlive , useKeepAliveRef } from 'keepalive-for-react' ;
4+ import { createElement , useMemo , useRef } from 'react' ;
45import { useTranslation } from 'react-i18next' ;
5- import { Outlet , useLocation , useNavigate } from 'react-router' ;
6- import { CSSTransition } from 'react-transition-group' ;
6+ import { useLocation , useNavigate , useOutlet } from 'react-router' ;
7+ import { CSSTransition , TransitionGroup } from 'react-transition-group' ;
78import { NavBar , Tabbar } from 'react-vant' ;
89import IconifyIcon from '@/components/Icon/IconifyIcon' ;
910import { constantRoutes } from '@/routers' ;
10- import { filterTabBar , searchRoute } from '@/routers/utils' ;
11+ import { filterKeepAlive , filterTabBar , searchRoute } from '@/routers/utils' ;
1112import { useSettingStore } from '@/store/setting' ;
1213
1314const Layout = ( ) => {
@@ -34,7 +35,7 @@ const Layout = () => {
3435 } ;
3536
3637 // 获取路由对象
37- const { pathname } = useLocation ( ) ;
38+ const { pathname, search } = useLocation ( ) ;
3839 const currentRoute = searchRoute ( pathname , constantRoutes ) ;
3940
4041 const isShowNavBar = ! currentRoute . meta ?. hiddenNavBar ;
@@ -54,6 +55,16 @@ const Layout = () => {
5455 // 使用i18n全局函数
5556 const { t } = useTranslation ( ) ;
5657
58+ const outlet = useOutlet ( ) ;
59+
60+ const aliveRef = useKeepAliveRef ( ) ;
61+
62+ const cacheKey = useMemo ( ( ) => {
63+ return pathname + search ;
64+ } , [ pathname , search ] ) ;
65+
66+ const cacheKeys = filterKeepAlive ( constantRoutes ) ;
67+
5768 return (
5869 < div className = "h-screen flex flex-col" >
5970 { isShowNavBar && (
@@ -71,19 +82,26 @@ const Layout = () => {
7182 />
7283 ) }
7384
74- < div className = "relative flex-1 overflow-x-hidden" >
75- < CSSTransition
76- key = { pathname }
77- nodeRef = { nodeRef }
78- appear
79- in = { isPageAnimate }
80- timeout = { 400 }
81- classNames = { getTransitionName ( ) }
82- >
83- < div ref = { nodeRef } className = "absolute inset-0 w-full" >
84- < Outlet />
85- </ div >
86- </ CSSTransition >
85+ < div className = "relative flex-1 overflow-hidden" >
86+ < KeepAlive activeCacheKey = { cacheKey } aliveRef = { aliveRef } include = { cacheKeys } >
87+ < TransitionGroup component = { null } >
88+ < CSSTransition
89+ key = { pathname }
90+ nodeRef = { nodeRef }
91+ appear
92+ in = { isPageAnimate }
93+ timeout = { 400 }
94+ classNames = { getTransitionName ( ) }
95+ >
96+ < div
97+ ref = { nodeRef }
98+ className = "absolute inset-0 w-full overflow-x-hidden overflow-y-auto"
99+ >
100+ { outlet }
101+ </ div >
102+ </ CSSTransition >
103+ </ TransitionGroup >
104+ </ KeepAlive >
87105 </ div >
88106
89107 { isShowTabBar && (
0 commit comments