Angular Routing路由前情提要

Nested (Child) Routes 子路由

路由跳轉時,讓component直接render在同樣頁面。

1. 子路由設定

在app.module.ts定義子路由:

1
2
3
4
5
6
const appRoutes: Routes =  [
...
{ path: 'users', component: UsersComponent, children: [
{ path: ':id', component: UserComponent },
] },
];

2. 使用router-outlet

架構是users底下有個別的user:

1
2
3
4
5
6
7
8
9
10
11
12
13
<div>
<a
[routerLink]="['/users', user.id]"
href="#"
*ngFor="let user of users">
{{ user.name }}
</a>
</div>
<div>
<!-- 從原來的app-user換成router-outlet -->
<!-- <app-user></app-user> -->
<router-outlet></router-outlet>
</div>