我正在使用react路由器(版本4.0.0),Router
组件有一个名为“history”的必需道具。
我知道在react-router的先前版本中,您可以从包中导入browserHistory
或hashHistory
并将其作为history
道具传递给<Router>
,但是在v4中这些都不可用。
项目可用的文档没有提及有关此必需历史记录here和here的内容,它引用了{4}}中不再提供的hashHistory
导出封装
有什么想法吗?
答案 0 :(得分:1)
如果要使用包含历史记录的组件,则应导入 来自&#39; react-router
的browserHistory或hashHistoryimport { Router,browserHistory } from 'react-router';
<Router history={browserHistory} routes={...} />
or
import { Router,hashHistory } from 'react-router';
<Router history={hashHistory} routes={...} />
答案 1 :(得分:0)
我导入的{ Router } from 'react-router-dom'
确实有一个必需的道具history
,但我需要导入没有必需道具历史记录的{ BrowserRouter as Router } from 'react-router-dom'
。这为我解决了这个问题。