尝试使用browserHistory.push方法以编程方式更改我的路线。

它将更改路线(按浏览器地址栏),但不会更新 View 。

这是我的代码
App.jsx

const AppStart = React.createClass({

  render: function() {
    return (
      <MuiThemeProvider>
        <Router history={hashHistory}>
          <Route path="/" component={Main}>
            <Route path="experiences" component={Experiences} />
            <Route path="people" component={Profiles} />
            <Route path="login" component={Login} />
            <IndexRoute component={Home}/>
          </Route>
        </Router>
      </MuiThemeProvider>
    );
  }
});

ReactDOM.render(
  <AppStart />,
  document.getElementById('app')
);

零件:
handleLoginRedirect(e) {
    browserHistory.push('/experiences');
  },

  render() {
    return (
      <div className='row'>
        <div className='col-sm-10 col-sm-offset-1'>
          <form role='form'>
           <RaisedButton label="Redirect" onClick={this.handleLoginRedirect} />
          </form>
        </div>
      </div>
    );

最佳答案

当您推送到hashHistory时,您的路由器配置将使用browserHistory

很容易错过这样的事情,这是可以理解的。

  • hashHistory替换为browserHistory中的Router,如下所示:
  • <Router history={browserHistory}>
    完整摘要:
    const AppStart = React.createClass({
    
      render: function() {
        return (
          <MuiThemeProvider>
            <Router history={browserHistory}>
              <Route path="/" component={Main}>
                <Route path="experiences" component={Experiences} />
                <Route path="people" component={Profiles} />
                <Route path="login" component={Login} />
                <IndexRoute component={Home}/>
              </Route>
            </Router>
          </MuiThemeProvider>
        );
      }
    });
    

    08-07 08:45