router-outlet and it's purpose
Understanding <router-outlet> in Angular and Its Purpose
In Angular, the <router-outlet> directive plays an important role in navigation and showing components dynamically based on the router configuration of the application. Let's see more about its purpose, how it works, and a few advanced use examples.
What is <router-outlet>?
<router-outlet> is an Angular built-in directive used as a placeholder for dynamically loaded routed components based on the application's router configuration. It acts as a viewport where Angular inserts the content of the active route.
Purpose of <router-outlet>
- Dynamic Component Loading: It allows Angular to render different components based on the URL path.
- Single Page Application (SPA): It helps build SPAs by loading only those parts of a page that are needed without reloading it entirely.
- Supports Nested Routing: Multiple <router-outlet> elements can be used to create child routes and complex navigation structures.
- Smooth Transitions: Enables seamless navigation between views without requiring a page refresh.
How <router-outlet> Works
Step 1: Define Routes in app-routing.module.ts
import { NgModule } from "@angular/core";
import { RouterModule, Routes } from "@angular/router";
import { HomeComponent } from "./home/home.component";
import { AboutComponent } from "./about/about.component";
const routes: Routes = [
{ path: "", component: HomeComponent },
{ path: "about", component: AboutComponent }
];
@NgModule({
imports: [RouterModule.forRoot(routes)]
})
export class AppRoutingModule { }
Step 2: Add <router-outlet>to app.component.html
<nav>
<a routerLink="/">Home</a>
<a routerLink="/about">About</a>
</nav>
<router-outlet></router-outlet>
- Clicking on Home (/) will load HomeComponent inside <router-outlet>.
- Clicking on About (/about) will load AboutComponent dynamically into <router-outlet>.
Using Multiple <router-outlet> for Nested Routing
If your application has a three-level navigation structure (Parent → Child → Grandchild), you can use multiple <router-outlet> elements.
Example: Parent-Child Routing
const routes: Routes = [
{
path: 'dashboard', component: DashboardComponent,
children: [
{ path: 'profile', component: ProfileComponent },
{ path: 'settings', component: SettingsComponent }
]
}
];
dashboard.component.html
<h2>Dashboard</h2>
<a routerLink="profile">Profile</a>
<a routerLink="settings">Settings</a>
<router-outlet></router-outlet> <!-- Child items are mounted here -->
- Enter /dashboard/profile, and DashboardComponent will be shown.
- ProfileComponent will be displayed inside the <router-outlet> inside DashboardComponent.
Named <router-outlet>for Multiple Views
You can use named outlets to render multiple views in different containers.
<router-outlet name="primary"></router-outlet>
<router-outlet name="sidebar"></router-outlet>
This way, you can create layouts with multiple views.
<router-outlet>is essential in Angular for navigation and dynamic component loading. It enables the creation of complex applications with nested routes, lazy loading, and multiple views.