What is router outlet in Angular?
In Angular, a router-outlet
is a directive that acts as a placeholder for the components to be displayed when a user navigates to a particular route. It is used in the HTML template to define where the content of a component associated with a specific route should be rendered.
For example:
<router-outlet></router-outlet>
In this code, the router-outlet
directive acts as a placeholder for the component associated with the active route. When the user navigates to a particular route, the component associated with that route is dynamically loaded and its template is rendered within the router-outlet
.
You can also use multiple router-outlet
directives to render components in different parts of the template:
<header>Header</header>
<nav>
<a routerLink="/home">Home</a>
<a routerLink="/about">About</a>
</nav>
<main>
<router-outlet></router-outlet>
</main>
<footer>Footer</footer>
In this code, the router-outlet
directive is used to render the component associated with the active route within the main
section of the template. The header and footer sections are outside of the router-outlet
, so they remain unchanged no matter which route the user navigates to.
In summary, the router-outlet
directive is used in Angular to define where the content of a component associated with a specific route should be rendered. It acts as a placeholder for the components to be displayed when a user navigates to a particular route.