From 492ca057cb87489949d0954a30dfd36ddd6ea1d7 Mon Sep 17 00:00:00 2001 From: ylj20011123 Date: Mon, 12 May 2025 15:31:30 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=92=A5=20feat(=E6=A8=A1=E5=9D=97):=20?= =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E4=BA=86=E4=B8=AA=E5=BE=88=E6=A3=92=E7=9A=84?= =?UTF-8?q?=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/components/KeepAliveOutlet/index.tsx | 15 +++++++++++---- src/models/user.ts | 5 +++++ 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/src/components/KeepAliveOutlet/index.tsx b/src/components/KeepAliveOutlet/index.tsx index 3375928..991bbeb 100644 --- a/src/components/KeepAliveOutlet/index.tsx +++ b/src/components/KeepAliveOutlet/index.tsx @@ -7,26 +7,33 @@ interface CacheItem { } // 创建一个缓存路由组件,用于保持路由组件的状态 -const KeepAliveOutlet: React.FC<{ global: any }> = (props) => { +const KeepAliveOutlet: React.FC<{ global: any, user: any }> = (props) => { const [cacheOutlets, setCacheOutlets] = useState([]); const location = useLocation(); const { pathname } = location; const currentOutlet = useOutlet(); const { tabsRoutes = [] } = props.global || {}; - + const { isLogin } = props.user || {}; + // 获取当前打开的所有标签页路径 const activePaths = tabsRoutes.map((tab: any) => tab.path); // 使用字符串比较来避免不必要的重渲染 const activePathsString = JSON.stringify(activePaths); - + useEffect(() => { // 如果当前路径已经在缓存中,则不需要再次添加 if (!cacheOutlets.find(item => item.pathname === pathname)) { setCacheOutlets(prev => [...prev, { outlet: currentOutlet, pathname }]); } }, [pathname, currentOutlet]); - + + useEffect(() => { + if (!isLogin) { + setCacheOutlets([]); + } + }, [isLogin]); + // 单独处理缓存清理逻辑,避免不必要的重渲染 useEffect(() => { // 清理已关闭标签页的缓存 diff --git a/src/models/user.ts b/src/models/user.ts index 81a8903..aad77ce 100644 --- a/src/models/user.ts +++ b/src/models/user.ts @@ -377,6 +377,11 @@ const UserModel: UserModelType = { }, }); + yield put({ + type: 'global/changeTabsRoutes', + payload: { data: [], action: 'remove' }, + }); + //当前页面不是登录页时,才进行重定向 if (window.location.pathname !== '/user/login') { // 只获取路径部分,不包含查询参数,避免redirect参数累积