我在使用react-router时报错,要求我用hashHistory,怎么写?

#1

我的路由代码:

ReactDOM.render((
  <Router>
  <Route path='/'          component={MainPage}></Route>
  <Route path='Login'      component={Login}></Route>
  <Route path='Register'   component={Register}></Route>
  <Route path='Homesource' component={Homesource}></Route>
  <Route path='Teamholl'   component={Teamholl}></Route>
  <Route path='Homelist'   component={Homelist}></Route>
  </Router>
  ),
  document.getElementById('GeekApp'),
);

报错信息:

Warning: [react-router] `Router` no longer defaults the history prop to hash history. Please use the `hashHistory` singleton instead. http://tiny.cc/router-defaulthistory

我想知道这个hashHistory应该怎么写?具体作用是什么?谢谢。

#2

就把用browserhistory的地方,或者这个单词换成hashhistory就行

1 Like
#3
// ...
import { Router, Route, hashHistory } from 'react-router'

render((
  <Router history={hashHistory}>
    <Route path="/" component={App}/>
  </Router>
), document.getElementById('app'))

官网的例子。
<Router> 应该要设置 history 的。

2 Likes
#4

对啊

#5

一开始我也遇到这样的问题了,设置了history={hashHistory}还是有warning。然后仔细看了一下,我竟然把hashHistory写成了hasHistory(尴尬的红脸 。不说了,面壁去了。。。

#6

如果你会BrowserRouter,直接替换成HashRouter即可。
参考这里:
http://blog.csdn.net/weixin_39424046/article/details/76222007

#7

这个感觉是没有传入history?你传递了history吗?
如?