2015-09-28 46 views
0

如上所述,我怎樣才能將它們一起使用?React-Router |我怎樣才能一起使用IndexRoute和重定向?版本:1.0.0-rc1

像這樣:

<Router history={history}> 
    <Route path="/" component={Root}> 
    <Route path='home' component={HomeContainer}></Route> 
    <Route path='about' component={AboutContainer}></Route> 

    <IndexRoute component={HomeContainer}/> 
    </Route> 
</Router> 

如果我需要的是服務於IndexRoute '/' 重定向到 '/家'?

我在react-router 1.0.0-rc1的文檔中找不到任何線索。

謝謝。


[編輯]

只是爲了闡述:

在上述結構中,當 '/' 被訪問時,該網頁將使用HomeContainer呈現;顯示的內容與訪問'/ home'時的內容完全相同。

所不同的是在瀏覽器地址欄中的網址:在前一種情況下的URL爲「/」,在後一種情況下的URL爲「/家」

我想的網址是相同的。所以我想要「/」(IndexRoute)直接重定向到「/ home」。

希望這可以說明我需要什麼。

PS: 我可以用一個虛設部件,只有做了重定向,實現的行爲:

<Router history={history}> 
    <Route path="/" component={Root}> 
    <Route path='home' component={HomeContainer}></Route> 
    <Route path='about' component={AboutContainer}></Route> 

    <IndexRoute component={DummyComponent}/> // which only does redirect 
    </Route> 
</Router> 

,並在DummyComponent:

componentDidMount() { 
    this.history.pushState(null, `/home`, null); 
}, 

但似乎一點點的解決方法。

+0

你能舉一些例子嗎?我發現很難理解你想要的行爲。 – Clarkie

+0

在問題中添加一些說明:) –

回答

0

對我來說最好的解決方案是使用onEnter函數。 (不知道這在2.0中如何工作,因爲我在2.0上使用它)。

<IndexRoute onEnter={(targetState, replace) => {replace('/home')}} /> 
相關問題