Introduction
Routing is one of the most important concepts in Angular applications. It allows the user to move between pages without reloading the browser. Instead of traditional server-based page navigation, Angular uses client-side routing, which provides a smooth single-page application experience.
If you are building an application that requires multiple screens like Dashboard, Login, Settings, or User Profile, then routing is essential.
In this article, we will learn routing from the ground level using a simple and understandable example: a small application with three pages:
Home Page
Products Page
Contact Page
By the end of this article, you will be able to:
Configure Angular Router
Navigate using Router Links
Use programmatic navigation
Pass route parameters
Implement a wildcard (404) route
Real-World Scenario
Imagine you are building a small ecommerce website. You want users to:
Land on the home page when they open the site
Click a menu link to view products
Click on a product to view details like Product ID
Navigate to a contact page for support
See a “Page Not Found” message if they enter an invalid URL
Routing helps make all this possible without reloading the entire application.
Step 1: Creating Components
We will create three components.
ng generate component home
ng generate component products
ng generate component contact
ng generate component product-details
ng generate component page-not-found
Step 2: Enable Routing During Project Creation
If routing was not enabled during project setup, Angular CLI generates a file named:
app-routing.module.ts
This file is where all routes are configured.
Step 3: Defining the Routes
Open app-routing.module.ts and add the following routes:
import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';
import { HomeComponent } from './home/home.component';
import { ProductsComponent } from './products/products.component';
import { ContactComponent } from './contact/contact.component';
import { ProductDetailsComponent } from './product-details/product-details.component';
import { PageNotFoundComponent } from './page-not-found/page-not-found.component';
const routes: Routes = [
{ path: '', component: HomeComponent },
{ path: 'products', component: ProductsComponent },
{ path: 'contact', component: ContactComponent },
{ path: 'products/:id', component: ProductDetailsComponent },
{ path: '**', component: PageNotFoundComponent }
];
@NgModule({
imports: [RouterModule.forRoot(routes)],
exports: [RouterModule]
})
export class AppRoutingModule { }
Explanation
''is the default route./products/:idreceives dynamic route parameters like product ID.**catches invalid or unknown routes (404 handling).
Step 4: Add Router Outlet
To display components based on routes, place a router-outlet tag inside app.component.html.
<nav>
<a routerLink="/">Home</a> |
<a routerLink="/products">Products</a> |
<a routerLink="/contact">Contact</a>
</nav>
<hr>
<router-outlet></router-outlet>
Angular will load components inside <router-outlet> based on navigation.

Join the conversation! Your thoughts help the community grow.