React router 2.6.0如何在单击函数内重定向

时间:2016-08-15 19:46:30

标签: javascript reactjs react-router

使用react-router时遇到问题。我想用firebase构建一个登录系统并做出反应。

当用户点击"登录"按钮和用户名和密码经过身份验证,页面将重定向到主页。

我的路线如下:



ReactDOM.render(
  <Router history={hashHistory}>
    <Route path='/'>
      <IndexRoute component={Index}></IndexRoute>
      <Route path='/start' component={Layout}></Route>
    </Route>
  </Router>,
app);
&#13;
&#13;
&#13;

Index组件是登录页面。

这是我的点击功能:

&#13;
&#13;
Jump(){
    var password = this.refs.password.value;
    var email = this.refs.username.value;
    console.log(email, password);
    
    firebase.auth().signInWithEmailAndPassword(email, password)
    .catch(function(error) {
      // Handle Errors here.
      var errorCode = error.code;
      var errorMessage = error.message;
      // ...
    })
    .then(function(res){
      if(res !== undefined){
        browserHistory.push('/#/start');
      //want to do the redirect here.
      }
    })

  }
&#13;
&#13;
&#13;

我尝试了browserHistory.push(),但它没有用。有人知道如何重定向到函数内部吗?真的很感激!

1 个答案:

答案 0 :(得分:1)

由于您在路由配置中使用hashHistory,因此您可以这样写它。

hashHistory.push('/start')

此外,路径配置中的路径不需要额外的斜杠。

更改

<Route path='/start' component={Layout}></Route>

<Route path='start' component={Layout}></Route>