💥 feat(模块): 添加了个很棒的功能

This commit is contained in:
ylj20011123 2025-05-12 15:31:30 +08:00
parent 9e41ad77de
commit 492ca057cb
2 changed files with 16 additions and 4 deletions

View File

@ -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<CacheItem[]>([]); const [cacheOutlets, setCacheOutlets] = useState<CacheItem[]>([]);
const location = useLocation(); const location = useLocation();
const { pathname } = location; const { pathname } = location;
const currentOutlet = useOutlet(); const currentOutlet = useOutlet();
const { tabsRoutes = [] } = props.global || {}; const { tabsRoutes = [] } = props.global || {};
const { isLogin } = props.user || {};
// 获取当前打开的所有标签页路径 // 获取当前打开的所有标签页路径
const activePaths = tabsRoutes.map((tab: any) => tab.path); const activePaths = tabsRoutes.map((tab: any) => tab.path);
// 使用字符串比较来避免不必要的重渲染 // 使用字符串比较来避免不必要的重渲染
const activePathsString = JSON.stringify(activePaths); const activePathsString = JSON.stringify(activePaths);
useEffect(() => { useEffect(() => {
// 如果当前路径已经在缓存中,则不需要再次添加 // 如果当前路径已经在缓存中,则不需要再次添加
if (!cacheOutlets.find(item => item.pathname === pathname)) { if (!cacheOutlets.find(item => item.pathname === pathname)) {
setCacheOutlets(prev => [...prev, { outlet: currentOutlet, pathname }]); setCacheOutlets(prev => [...prev, { outlet: currentOutlet, pathname }]);
} }
}, [pathname, currentOutlet]); }, [pathname, currentOutlet]);
useEffect(() => {
if (!isLogin) {
setCacheOutlets([]);
}
}, [isLogin]);
// 单独处理缓存清理逻辑,避免不必要的重渲染 // 单独处理缓存清理逻辑,避免不必要的重渲染
useEffect(() => { useEffect(() => {
// 清理已关闭标签页的缓存 // 清理已关闭标签页的缓存

View File

@ -377,6 +377,11 @@ const UserModel: UserModelType = {
}, },
}); });
yield put({
type: 'global/changeTabsRoutes',
payload: { data: [], action: 'remove' },
});
//当前页面不是登录页时,才进行重定向 //当前页面不是登录页时,才进行重定向
if (window.location.pathname !== '/user/login') { if (window.location.pathname !== '/user/login') {
// 只获取路径部分不包含查询参数避免redirect参数累积 // 只获取路径部分不包含查询参数避免redirect参数累积