2017-01-07 19 views
1

我正在創建一個應用程序,其中包含2個組件的單個路徑。這裏是我的路線:在auth的情況下,React-router多組件onEnter?

<Router history={hashHistory}> 
    <Route path="/" component={AppContainer} onEnter={requireEnter}> 
     <Route path="/homepage" component={HomePage} /> 
     <Route component={MainPage} onEnter={requireAuth}> 
      <Route path="/home" component={DashBoard} /> 
     </Route> 
    </Route> 
</Router> 

在這種情況下,我使用的是AppContainer成分進入我的應用程序。而不是我希望那個組件應該是我的選擇,因爲我有一個主頁,將有path="/"

眼下onEnter={requireEnter}正在處理包括以下情況:

function requireEnter (nextState, replace) { 
    if (nextState.location.pathname == "/") { 
     if (checkLoggedIn()) { 
      replace({ 
       pathname: '/home' 
      }) 
     } else { 
      replace({ 
       pathname: '/homepage' 
      }) 
     } 
    } 
} 

但我想這樣的事情:

function requireEnter (nextState, replace) { 
    if (nextState.location.pathname == "/") { 
     if (checkLoggedIn()) { 
      //component should be AppContainer and redirect to '/home' 
     } else { 
      //component should be home page 
     } 
    } 
} 

回答

0

您可以添加路由,在它沒有分量,已在使用它另一個類似的情況:

<Route path="/" onEnter={requireEnter}> 
    <Route path="home" component={AppContainer}> 
     <IndexRoute component={HomeContainer} /> 
    </Route> 
    <Route path="homepage" component{HomePageContainer}> 
    </Route> 
</Route> 

然後和往常一樣:

function requireEnter (nextState, replace){ 
    if(nextState.location.pathname=="/"){ 
     if(checkLoggedIn()){ 
     replace({ 
       pathname: '/home' 
      }); 
     }else{ 
     replace({ 
       pathname: '/homepage' 
     }); 
     } 
    } 
}