[英]How to properly setup nested routing in angular
I'm trying to setup angular route with nested path.我正在尝试使用嵌套路径设置角度路线。 parent page is
/list
child page is /list/:id
父页面是
/list
子页面是/list/:id
for some reason, the following route doesnt work.出于某种原因,以下路线不起作用。 if I navigate to
/list/:id
, the route is found, but it loads ListComponent
instead of ViewComponent
如果我导航到
/list/:id
,会找到路由,但它会加载ListComponent
而不是ViewComponent
{
path: 'list',
component: ListComponent,
children: [
{
path: ':id',
component: ViewComponent
}
]
}
I'm pretty sure this is the same setup as the example in angular doc .我很确定这与angular doc 中的示例设置相同。 am I missing something?
我错过了什么吗?
the following works, but it has the wrong hierarchy, /list
is siblings to /list/:id
以下有效,但它的层次结构错误,
/list
是/list/:id
兄弟
{
path: 'list',
children: [
{
path: '',
component: ListComponent
},
{
path: ':id',
component: ViewComponent
}
]
}
In the first example, since ViewComponent is a child of ListComponent, you have to add <router-outlet></router-outlet>
in list.component.html, based on your stackblitz example:在第一个示例中,由于 ViewComponent 是 ListComponent 的子项,因此您必须根据您的 stackblitz 示例在 list.component.html 中添加
<router-outlet></router-outlet>
:
<p>
list works!
</p>
<a [routerLink]="['./view']">view</a>
or
<button (click)="buttonClick()">view</button>
<!-- add the line below here-->
<router-outlet></router-outlet>
ViewComponent is here loaded where you place the router outlet. ViewComponent 被加载到你放置路由器插座的地方。
This will make angular to load both ListComponent and ViewComponent when you go to the route /list/:id, since ViewComponent is a child of ListComponent.当您转到路由 /list/:id 时,这将使 Angular 加载 ListComponent 和 ViewComponent,因为 ViewComponent 是 ListComponent 的子项。
Every route with children, needs to have the <router-outlet></router-outlet>
tag.每条有孩子的路线,都需要有
<router-outlet></router-outlet>
标签。
This architecture is useful for example (and many other types of situations) if ListComponent is a navigation sidebar, and ViewComponent contains data from backend.例如,如果 ListComponent 是导航侧栏,并且 ViewComponent 包含来自后端的数据,则此架构很有用(以及许多其他类型的情况)。
Edit编辑
If you don't want to load both ListComponent and ViewComponent when you visit the /list/:id route, you could change your routes to look like this:如果您不想在访问 /list/:id 路由时同时加载 ListComponent 和 ViewComponent,则可以将路由更改为如下所示:
const mainRoutes: Routes = [
{
path: 'list',
component: ListComponent,
},
{
path: 'list/:id',
component: ViewComponent
}
];
When you are going with this architecture only ViewComponent is loaded when you visit 'list/:id', since ViewComponent is not a child of ListComponent.当您使用此架构时,仅在您访问“list/:id”时加载 ViewComponent,因为 ViewComponent 不是 ListComponent 的子项。
In this example you shouldn't use the router-outlet in list.component.html在这个例子中,你不应该在 list.component.html 中使用 router-outlet
Since ViewComponent is a child component, you've to render it inside parent component.由于 ViewComponent 是一个子组件,您必须在父组件中渲染它。 You can do it by adding
<router-outlet></router-outlet>
in the ListComponent.您可以通过在 ListComponent 中添加
<router-outlet></router-outlet>
来实现。 It will solve your problem.它会解决你的问题。
Previously, in a project, I have done it the following way (I changed the names of the components to match your components):以前,在一个项目中,我是通过以下方式完成的(我更改了组件的名称以匹配您的组件):
const routes: Routes = [
{
path : 'list',
component: ListComponent
},
{
path : 'list/:id',
component: ViewComponent
},
{
path : '**',
component: ListComponent
}
];
And then in the imports:然后在进口中:
@ngModule({
imports: [RouterModule.forChild(routes)]
})
Hope that helps.希望有帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.