简体   繁体   English

Angular2 路由,父子节点——路由组件 vs 视图组件

[英]Angular2 routing, parent child -- route component vs view component

I'm working on an Angular2 application, currently we're on beta-17 since the release candidate no longer supported bundling and broke everything (that's another story).我正在开发一个Angular2应用程序,目前我们处于 beta-17,因为候选版本不再支持捆绑并破坏了一切(那是另一个故事)。

I understand that the relationship between parent and child routes are expected to be that the child must be a "routing component" if it wants to have routes.我理解父子路由之间的关系应该是子路由如果想要路由就必须是“路由组件”。 That makes sense, right -- but what if I want to have a parent "routing component", that has a child "view component" which acts as a parent to a child "routing component"?这是有道理的,对 - 但是如果我想要一个父“路由组件”,它有一个子“视图组件”,它充当子“路由组件”的父级?

.
├── app.component // Has two child routes, both are "view components"
     ├── plan.component
     └── design.component // View component, route "/design/:id/..."
         ├── header.component
         ├── nav-and-body.component // Routing component
         └── footer.component

I have a top-level app that has two routes, the /plan/:id/... and /design/:id/... routes.我有一个顶级应用程序,它有两条路线, /plan/:id/.../design/:id/...路线。 As you can see I have a desire that these will serve as parents to other "routing components".如您所见,我希望这些将作为其他“路由组件”的父级。 Imagine we're in the "design" route, and we attempt to navigate to a child route defined in the NavAndBodyComponent it doesn't work, nor does it throw an error.想象一下,我们在“设计”路线中,我们尝试导航到NavAndBodyComponent定义的子路线,但它不起作用,也不会抛出错误。

I believe this is due to the fact that I have a parent "routing component", that has a child "view component", which has a nested "routing component" - how can I fix this without changing this hierarchy?我相信这是因为我有一个父“路由组件”,它有一个子“视图组件”,它有一个嵌套的“路由组件”——我怎样才能在不改变这个层次结构的情况下解决这个问题?

The view components load as expected:视图组件按预期加载:

URL: localhost:5000/design/10网址: localhost:5000/design/10

But when I attempt navigation to the nested child route the URL changes to但是当我尝试导航到嵌套的子路由时,URL 更改为

URL: localhost:5000/model网址: localhost:5000/model

I want (and expected):我想要(和预期):

URL: localhost:5000/design/10/model网址: localhost:5000/design/10/model

Update更新

Here is a very detailed Plunker !这是一个非常详细的Plunker

The problem is caused by adding ROUTER_PROVIDERS on a component other than the root component.该问题是由在根组件以外的组件上添加ROUTER_PROVIDERS引起的。

Plunker example Plunker 示例

How to prevent adding dynamically added routes each time a route is activated see Angular2: Configuration 'name' conflicts with existing route 'name'如何防止每次激活路由时添加动态添加的路由,请参见Angular2:配置“名称”与现有路由“名称”冲突

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM