反应路由不按预期工作

时间:2017-05-04 16:15:44

标签: reactjs react-router

我的定义如下:

<Route path="/" onEnter={this.verifyLang}>
  <Route path="/:lang" onEnter={this.verifyLang}>

    <IndexRoute component={this.getIndexRoute()} />
    <Route path="auth">
      <Route path="confirm-password" component={this.getIndexRoute()} />
      <Route path="confirm-code/code=:code&email=:email" component={this.getIndexRoute()}  />
      <Route path="confirm_email" component={this.getIndexRoute()}  />
    </Route>
    <Route path="landing" component={Landing} />
    <Route path="impress" component={Impress} />

  </Route>
</Route>

函数this.getIndexRoute()为我提供了Launch组件。现在在Launch组件中我有一个方法:

componentWillReceiveProps(nextProps){

    browserHistory.push('landing');
}

我希望重定向到Landing组件,但没有任何反应。为什么会这样?

1 个答案:

答案 0 :(得分:0)

而不是......

<IndexRoute component={this.getIndexRoute()} />

我会用......

<IndexRedirect to='landing' />

重构代码:

<Route path="/" onEnter={this.verifyLang}>

  <Route path=":lang" onEnter={this.verifyLang}>

    <IndexRedirect to='landing' />

    <Route path="auth">
      <Route path="confirm-password" component={this.getIndexRoute()} />
      <Route path="confirm-code/code=:code&email=:email" component={this.getIndexRoute()}  />
      <Route path="confirm_email" component={this.getIndexRoute()}  />
    </Route>

    <Route path="landing" component={Landing} />
    <Route path="impress" component={Impress} />
  </Route>
</Route>