react中缓存组件的方法

140 阅读1分钟

react缓存组件的方法,不像vue中直接有现成的组件可以使用(keep-alive),react中需要借助插件来实现

  1. 安装插件keepalive-react-component
  2. 在routes.js中引入,scroll设置true表示缓存滚动条的位置
import {withKeepAlive } from 'keepalive-react-component'

const routes = [{
    path: '/',
    name: 'home',
    component: withKeepAlive(Home, {cacheId: 'home', scroll: true}),
}]