其实是项目升级引起的问题,对umi4的直接使用者来讲只能算是用法。
在umi3中,只需从umi导入keepAlive即可使用,但升级到umi4之后,这样使用报错了。
第一版解决方案是(实际是错误的)将导入改为从"react-activation"导入,同时导入AliveScope做一层包裹
<AliveScope>
<KeepAlive autoFreeze={false} saveScrollPosition="screen">
内容...
</KeepAlive>
</AliveScope>
这样不在报错,但是缓存失效了。
真正的解决方案,umi4的keepAlive还要搭配上一个插件来使用。同时加一个key。 例如:
package.json下
"umi-plugin-keep-alive": "^0.0.1-beta.35",
config.ts下
plugins: [
'umi-plugin-keep-alive',
],
直接使用keepAlive即可
<KeepAlive cacheKey={history.location.pathname} autoFreeze={false} saveScrollPosition="screen">
内容...
</KeepAlive>