【发布时间】:2017-06-04 06:39:39
【问题描述】:
我有一个带有 react-router 的 react 应用程序。我正在尝试设置嵌套路由:
"/" --> home page
"/products" --> products list (child component of home page)
"/products/new" --> new product: child component of products list
到目前为止我尝试做的事情:
<Route path="/" component="home" >
<Route path="products" component="products" >
<Route path="new" component="products_new" />
</Route>
</Route>
现在在我的默认主页的浏览器中,当我点击"/products" 时,产品组件已加载,我可以看到我的产品列表。但是当我点击"products/new" 时,什么也没有发生。我得到一个空白页。如果我点击"/new"(未嵌套)它可以工作(页面 product_new 加载到其父级中)。
(这个"/products/new" 不起作用;这个"/new" 起作用)
我在 github 上查看了这个问题 Problem with nested routes #687。解决方案说:
我发现了我的问题。总是调用父路由。那就是 意图。但是子组件需要重复
<Router.RouteHandler/>得到渲染。
我无法理解这个解决方案。这是什么意思:
"但是子组件需要重复
<Router.RouteHandler/> 被渲染”
EDIT1: 这是我的组件(路由器和视图):
-
我的路由层次结构:
<Route path="/" > <IndexRoute component={Home}/> <Route path="products"> <IndexRoute component={Products} /> <Route path="new" component={Products_New} /> </Route> </Route> </Router> -
我的家庭组件:
<div className="col-lg-12"> <h1>Home page</h1> <hr /> {this.props.children} </div> -
我的产品组件:
<div> <div className="col-lg-12"> <h1>Products</h1> </div> <hr /> {this.props.children} </div> - 我的产品新组件:
【问题讨论】:
标签: reactjs react-router nested-routes