2016-07-08 78 views
1

我写了一个应用程序来教自己Flux和React路由器,并且我陷入了嵌套路线。我有一个公园列表,当你点击其中一个公园时,它会出现一个显示页面。这工作正常,如果我有我的路线嵌套像这样:嵌套在IndexRoute

const appRouter = (
    <Router history={ hashHistory }> 
    <Route path="/" component={ App }> 
     <IndexRoute component={ AppIndex }/> 
     <Route path="parks/:parkId" component={ ParkShow }/> 
    </Route> 
    </Router> 
) 

但是,那不是我想要的。我想要显示在AppIndex页面中点击的公园的信息。这就是我要实现这一目标:

const appRouter = (
    <Router history={ hashHistory }> 
    <Route path="/" component={ App }> 
     <IndexRoute component={ AppIndex }> 
     <Route path="parks/:parkId" component={ ParkShow }/> 
     </IndexRoute> 
    </Route> 
    </Router> 
) 

我得到 - “警告:[反应路由器]的位置‘公园/ 2’不匹配任何路线”

而且 - “警告:反应路由器]的位置“/园区/ 2”内的索引页是一个错误的方式

const appRouter = (
    <Router history={ hashHistory }> 
    <Route path="/" component={ App }> 
     <IndexRoute component={ AppIndex }/> 
     <Route path="parks/:parkId" component={ ParkShow }/> 
    </Route> 
    </Router> 
) 

上面一层是唯一正确途径 设计应用在这样的不匹配任何路线”

+0

一种解决方案是将AppIndex组件添加到我的ParkShow组件的顶部。这给了我正在寻找的行为,但对我来说似乎很难受。我觉得必须有办法从路线本身获得这种行为。 –

回答

2

路线一个 将AppIndex的内容移动到APP并在APP组件中使用this.props.children。 您应该显示Appindex或ParkShow

+0

这不会产生我想要的行为。 我希望的行为是让AppIndex中的内容在访问主页时显示出来,并坚持到ParkShow,而不是坚持到应用中的其他页面,而我希望App组件中的内容在整个应用中持续存在。 –

+0

我在这里发布了一个后续问题:http://stackoverflow.com/questions/38276787/how-to-nest-react-routes-in-homepage –