React Router与嵌套路由不匹配

时间:2017-03-15 14:07:33

标签: javascript reactjs react-router react-redux

我遇到了React Router的小问题。我有以下路由配置:

ReactDOM.render(
    <Provider store={store}>
        <Router history={browserHistory}>
            <Route path="/" component={ProjectManagerApp}>
                <IndexRoute component={MainDashboard} />
                <Route path='/projects/:id' component={ProjectView}>
                    <IndexRoute component={ProjectPanel} />
                    <Route path='/todos' component={ProjectTodos} />                    
                </Route>                
                <Route path='*' component={NotFound} />            
            </Route>
        </Router>
    </Provider>
    ,
    document.getElementById('app')
);

问题在于嵌套在“ProjectView”中的路由。 IndexRoute正在工作,因此,当我访问“http://localhost:3000/projects/38d38234-5bdb-474a-8db8-6206cefe3e87”时,会呈现项目面板组件。但是当我访问“http://localhost:3000/projects/38d38234-5bdb-474a-8db8-6206cefe3e87/todos”时,它会落在NotFound路线上。每个其他路线都正常工作。我正在使用React Router 3.有人可以帮助我吗?

由于

1 个答案:

答案 0 :(得分:2)

您使用trailing /指定的所有路线都将相对于根,因此

<Route path='/todos' component={ProjectTodos} />

可以使用http://localhost:8080/todos事件访问,但它是嵌套路径

要访问您想要的方式,请删除尾随/。指定您的路线,如

ReactDOM.render(
    <Provider store={store}>
        <Router history={browserHistory}>
            <Route path="/" component={ProjectManagerApp}>
                <IndexRoute component={MainDashboard} />
                <Route path='projects/:id/' component={ProjectView}>
                    <IndexRoute component={ProjectPanel} />
                    <Route path='todos' component={ProjectTodos} />                    
                </Route>                
                <Route path='*' component={NotFound} />            
            </Route>
        </Router>
    </Provider>
    ,
    document.getElementById('app')
);