Basic Routing Setup
Navigate between different pages without a full page reload. Angular Router handles it all.
Routing lets you show different pages/components without reloading the browser. The URL changes, the component changes, but the browser never refreshes.
"Routing = TSRTC bus system โ URL bus number, component destination."
Single Page Application (SPA) means one HTML file that can show multiple "views" โ the router swaps components in and out as the user navigates.
Without routing: clicking a link reloads the entire page, loses all state, shows a flash of white.
With routing: clicking a link changes the URL, swaps the component smoothly, keeps the header/footer, preserves state.
Routes are defined in an array and provided to the app via provideRouter().
// app.routes.ts
import { Routes } from '@angular/router';
import { HomeComponent } from './home/home.component';
import { MenuComponent } from './menu/menu.component';
import { AboutComponent } from './about/about.component';
export const routes: Routes = [
{ path: '', component: HomeComponent },
{ path: 'menu', component: MenuComponent },
{ path: 'about', component: AboutComponent },
];
// app.config.ts
import { provideRouter } from '@angular/router';
import { routes } from './app.routes';
export const appConfig: ApplicationConfig = {
providers: [
provideRouter(routes),
// other providers...
]
};
"Routes array = bus route chart โ kahan se kahan jana hai."
Each route has a path (URL segment) and a component (what to show when that URL matches).
<router-outlet> is the placeholder where Angular inserts the matched component.
// app.component.html
<header>
<h1>BiryaniHub</h1>
<nav>
<a routerLink="/menu">Menu</a>
<a routerLink="/about">About</a>
</nav>
</header>
<!-- Router outlet โ component swaps here -->
<router-outlet />
<footer>
<p>ยฉ 2025 BiryaniHub</p>
</footer>
"RouterOutlet = bus stand โ jo bhi bus aaye, yahan khada hoga."
Everything around <router-outlet> (header, footer, sidebar) stays constant. Only the content inside the outlet changes when the route changes.
routerLink is the Angular way to navigate โ it changes the URL without reloading the page.
<!-- Declarative navigation -->
<a routerLink="/menu">Menu</a>
<!-- With params (array syntax) -->
<a [routerLink]="['/menu', biryaniId]">Details</a>
<!-- Active link highlighting -->
<a routerLink="/menu" routerLinkActive="active"
[routerLinkActiveOptions]="{ exact: true }">Menu</a>
"routerLink = bus ticket โ URL change karo bina page reload kiye."
NEVER use href="/menu" for internal navigation โ that reloads the whole page and defeats the SPA purpose. Always use routerLink.
routerLinkActive adds a CSS class when the current URL matches the link โ perfect for highlighting the active nav item.
Sometimes you need to navigate from code (after form submit, after login, etc.). Use the Router service.
import { Component, inject } from '@angular/core';
import { Router } from '@angular/router';
@Component({...})
export class LoginComponent {
private router = inject(Router);
login() {
// After successful login
this.router.navigate(['/dashboard']);
}
goBack() {
this.router.navigateByUrl('/menu');
}
search(sortBy: string) {
this.router.navigate(['/menu'], {
queryParams: { sort: sortBy }
});
}
}
"Programmatic = driver bolke bhejna โ code se route change karo."
navigate() takes an array of path segments (like ['/menu', '42'] for /menu/42). navigateByUrl() takes a full URL string.
Key Takeaways
- โ Routing swaps components based on URL without page reload (SPA)
- โ Routes are defined in an array and provided with provideRouter(routes)
- โ <router-outlet> is the placeholder where matched components render
- โ routerLink navigates without reload; routerLinkActive highlights active link
- โ Use inject(Router).navigate([...]) for programmatic navigation from code
Want to track your progress?
Log in to save your place and pick up where you left off.
Progress track karna chahte ho?
Login karo apni progress save karne ke liye aur jahan chhoda tha wahan se shuru karo.
Login