2017-07-05 48 views
0

App.js反應路由器4 - 嵌套switch預期

<ConnectedRouter history={history}> 
    <Switch> 
     <Route path="/dashboard" name="Layout" component={Layout} /> 
     <Route exact path="/login" name="Login" component={Login} /> 
     <Redirect from="/" to="/dashboard" /> 
     <Route component={NoMatch}/> 
    </Switch> 
</ConnectedRouter> 

Layout.js

<Switch> 
    <Route path="/" name="Dashboard" component={Dashboard} /> 
    <Route path="/components/a" component={ComponentA} /> 
    <Route path="/components/b" component={ComponentB} /> 
</Switch> 
  • 問題不起作用:
    • 問題#1導航到「/dashboard/components/a'或'/ dashboard/components/b'不按預期顯示組件a或b
    • 問題#2導航到錯誤的路徑總是重定向到 '/儀表盤' 不顯示NOMATCH部件

UPDATE: 改變重定向到 <Route exact path='/' render={() => <Redirect to='/dashboard' />} /> 可以修復問題#2問題#1仍然

+0

嘗試使用'<重定向從= '/' 來= 「/組件/一個」/>'後'<路由路徑=「/ components/a「component = {ComponentA} />' –

+0

嘗試在您的路線上放置'exact'? – Winter

回答

0

您可能需要更改Route路徑以擁有一個/dashboard PARAM以及當你tyring導航到/dashboard/componenets/a/dashboard/componenets/b

<Switch> 
    <Route exact path="/dashboard" name="Dashboard" component={Dashboard} /> 
    <Route path="/dashboard/components/a" component={ComponentA} /> 
    <Route path="/dashboard/components/b" component={ComponentB} /> 
</Switch> 
+0

這不工作。這個問題可能是由於} />',但如果我刪除'exact',顯示空白頁? – user3765825

+0

如果您只是使用以上配置 –

+0

我試過了,但它沒用 – user3765825