Angular 2:在路由出口中让子路由组件替换父路由组件 [英] Angular 2: Have Child Route Component Replace Parent in router-outlet
问题描述
是否可以在父路径的<router-outlet>
中显示子路径?
Is there a way to display a child route in the parent route's <router-outlet>
?
例如,假设我们有两条路线:
For example, let's say we have two routes:
/users/12345
/users/12345/orders/12345
我需要第二个成为第一个的子代,但我还需要用/users/12345/orders/12345
的内容完全替换/users/12345
的内容,而不是在/users/12345
子router-outlet
中具有/users/12345/orders/12345
I need the second to be a child of the first, but I also need to completely replace the contents of /users/12345
with the contents of /users/12345/orders/12345
opposed to having /users/12345/orders/12345
in the /users/12345
sub router-outlet
.
我以为我可以通过仅命名父级路由器出口并让两个路由都针对它来做到这一点,但是从我一直在做的研究(及其引起的错误)中,我感觉到了名称的用途已经存在主要路由器出口的情况下用于次要用途
I thought I could do this by just naming the parent level router-outlet and have both routes target it, but from the research I've been doing (and the errors it caused) I get the feeling the names were intended for secondary uses when there already exists a primary router-outlet
推荐答案
我遇到了同样的问题.这是我解决的方法:
I had the same issue. This is how I fixed it:
const routes: Routes = [
{
path: '',
children: [
{
path: '',
component: ParentComponent,
},
{
path: 'child',
component: ChildComponent,
}
]
}
];
这篇关于Angular 2:在路由出口中让子路由组件替换父路由组件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!