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参数累积