2017-05-04 146 views
0

我有一個像下面的定義我的根:反應路由工作不正常

<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()給我啓動組件。現在在啓動組件我有一個方法:

componentWillReceiveProps(nextProps){ 

    browserHistory.push('landing'); 
} 

我期待着重定向到着陸組件,但沒有任何反應。爲什麼是這樣?

+0

路由器什麼版本的?那是你引用的react-router'browserHistory'嗎? –

+0

正確我猜它不是版本3 – Nitish

+0

http://stackoverflow.com/a/31079244/438992所以如果一切都很好,我不知道這個問題會是什麼。 –

回答

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>