安装
npm install react-router-cache-route --save # or yarn add react-router-cache-route
使用方法
使用 CacheRoute
替换 Route
使用 CacheSwitch
替换 Switch
(因为 Switch
组件只保留第一个匹配状态的路由,卸载掉其他路由)
import React from 'react' import { HashRouter as Router, Route } from 'react-router-dom' import CacheRoute, { CacheSwitch } from 'react-router-cache-route'import List from './views/List' import Item from './views/Item'const App = () => (<Router><CacheSwitch><CacheRoute exact path="/list" component={List} /><Route exact path="/item/:id" component={Item} /><Route render={() => <div>404 未找到页面</div>} /></CacheSwitch></Router> )export default App
案例地址:https://github.com/sf198936/react-router5-cachRoute