Dynamically Adding Route Components
Rather than define each route's component separately, use RouterOutlet which serves as a component placeholder; Angular dynamically adds the component for the route being activated into the <router-outlet></router-outlet> element.
1
import { Component } from '@angular/core';
2
3
@Component({
4
selector: 'app',
5
template: `
6
<nav>
7
<a routerLink="/component-one">Component One</a>
8
<a routerLink="/component-two">Component Two</a>
9
</nav>
10
11
<router-outlet></router-outlet>
12
<!-- Route components are added by router here -->
13
`
14
})
15
export class AppComponent {}
Copied!
In the above example, the component corresponding to the route specified will be placed after the <router-outlet></router-outlet> element when the link is clicked.
Last modified 9mo ago
Copy link