react缓存组件的方法,不像vue中直接有现成的组件可以使用(keep-alive),react中需要借助插件来实现
- 安装插件
keepalive-react-component - 在routes.js中引入,scroll设置true表示缓存滚动条的位置
import {withKeepAlive } from 'keepalive-react-component'
const routes = [{
path: '/',
name: 'home',
component: withKeepAlive(Home, {cacheId: 'home', scroll: true}),
}]