ant-design-pro路由缓存

一.ant-design-pro v5版本

1.app.tsx中引入KeepAlive

import { KeepAlive } from 'react-activation';

2.在childrenRender中配置(红框中是不需要加入缓存的路由数据)

  {

            props.location?.pathname !== '/' && props.location?.pathname !== '/user/login' && props.location?.pathname !== '/welcome/welcome' ?

              <KeepAlive

                when={true} //组件卸载后缓存(离开页面时缓存)可以传入Boolean,array,function

                id={props.location?.pathname} // 缓存多个页面时要有id

                name={props.location?.pathname} // 页面的path地址

                saveScrollPosition="screen" // 可以记住页面的滚动位置,切换页面是回到上一次滚动的位置,可以设置为false

              >

                {children}

              </KeepAlive> :

              children

}

3.清除缓存数据(有需要的话)

import { useAliveController } from 'react-activation';

 const { dropScope } = useAliveController();

 dropScope('路由数据');

二.ant-design-pro v6版本

1.安装依赖

npm install react-activation
# 或
yarn add react-activation

2.app.tsx中配置

import { KeepAlive, AliveScope } from 'react-activation';

import { useLocation } from '@umijs/max';

(keepAlive需要用AliveScope进行包裹,缓存才会生效)

3.清理缓存

import { useAliveController } from 'react-activation';

const { dropScope } = useAliveController();

dropScope('路由数据名称');

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值