Full answer
Routing lets one page show different components depending on the URL, without a full page reload.
Set-up has four pieces:
- A
Routesarray maps paths to components:{ path: 'courses/:id', component: CourseDetailComponent }. provideRouter(routes)in theApplicationConfigproviders turns routing on.<router-outlet />in a template marks where the matched component is rendered. ImportRouterOutletin that component.routerLink="/courses"on links navigates without reloading.routerLinkActive="active"adds a CSS class on the current link. ImportRouterLinkandRouterLinkActive.
Route parameters. In courses/:id, the part after the slash is a parameter. There are two ways to read it: the older ActivatedRoute (route.paramMap observable or route.snapshot.paramMap), or the newer way: add withComponentInputBinding() to provideRouter, and the parameter is bound to an input called id. Query parameters and resolver data can be bound in the same way.
Order matters. The router picks the first match, so the wildcard { path: '**', component: NotFoundComponent } must be last. A redirect from the empty path needs pathMatch: 'full', or it matches every URL.
To navigate from code, inject Router and call router.navigate(['/courses', id]).
Extra points that come up in interviews:
- Query parameters and the fragment are separate from path parameters:
/courses?level=easy#reviews. Navigate with[queryParams]onrouterLink, orrouter.navigate(['/courses'], { queryParams: { level: 'easy' } }). They stay optional and are not part of the route match. routerLinkaccepts an array:[routerLink]="['/courses', course.id]"builds/courses/42and encodes special characters safely.- Relative links such as
routerLink="../"are resolved against the current route, which is useful in nested routes. - Lazy load a page with
loadComponent: () => import('./course-detail').then(m => m.CourseDetailComponent). The code for that page is downloaded only when the user visits it. - A
titleon a route sets the document title, andredirectTowithpathMatch: 'full'sends''to a default page. - Guards (
canActivate) protect routes, and resolvers load data before the page shows. These are separate topics, but a good answer names them. - For a link to show as active, use
routerLinkActiveand, for the home link,[routerLinkActiveOptions]="{ exact: true }", otherwise/is active on every page.
If a parameter changes while the same component stays on screen (from /courses/1 to /courses/2), Angular reuses the component. With input binding the input updates, so use a computed or effect on it and not ngOnInit.
// file: app.routes.ts
import { Routes } from '@angular/router';
import { CourseDetailComponent } from './course-detail.component';
import { CourseListComponent } from './course-list.component';
import { NotFoundComponent } from './not-found.component';
export const routes: Routes = [
{ path: '', redirectTo: 'courses', pathMatch: 'full' },
{ path: 'courses', component: CourseListComponent, title: 'Courses' },
{ path: 'courses/:id', component: CourseDetailComponent },
{ path: '**', component: NotFoundComponent },
];
// file: app.config.ts
import { ApplicationConfig } from '@angular/core';
import { provideRouter, withComponentInputBinding } from '@angular/router';
import { routes } from './app.routes';
export const appConfig: ApplicationConfig = {
providers: [provideRouter(routes, withComponentInputBinding())],
};
// file: app.component.ts
import { Component } from '@angular/core';
import { RouterLink, RouterLinkActive, RouterOutlet } from '@angular/router';
@Component({
selector: 'app-root',
imports: [RouterOutlet, RouterLink, RouterLinkActive],
template: `
<nav>
<a routerLink="/courses" routerLinkActive="active">Courses</a>
</nav>
<router-outlet />
`,
})
export class AppComponent {}
// file: course-list.component.ts
import { Component } from '@angular/core';
import { RouterLink } from '@angular/router';
@Component({
selector: 'app-course-list',
imports: [RouterLink],
template: `<a [routerLink]="['/courses', 42]">Angular basics</a>`,
})
export class CourseListComponent {}
// file: course-detail.component.ts
import { Component, input } from '@angular/core';
@Component({
selector: 'app-course-detail',
template: `<h1>Course {{ id() }}</h1>`,
})
export class CourseDetailComponent {
// Filled from the :id route parameter by withComponentInputBinding().
id = input.required<string>();
}
// file: not-found.component.ts
import { Component } from '@angular/core';
@Component({
selector: 'app-not-found',
template: `<p>Page not found</p>`,
})
export class NotFoundComponent {}Angular 20.3. In `main.ts` pass `appConfig` to `bootstrapApplication`. The `id` input is filled from the URL `/courses/42`.
Key points
- `provideRouter(routes)` plus `<router-outlet />` plus `routerLink` are the core pieces.
- `:id` defines a parameter. `withComponentInputBinding()` delivers it as an input.
- First match wins, so put `**` last and use `pathMatch: 'full'` on empty-path redirects.
- Navigate in code with `Router.navigate`.
Common mistakes
- Using `href` on links. That reloads the whole page and loses app state.
- Putting the `**` route first, which makes every route show the not-found page.
- Forgetting `pathMatch: 'full'` on a `path: ''` redirect.
Follow-ups
- What is the difference between `paramMap` and `snapshot.paramMap`?
- How do query parameters differ from route parameters?
- How do you make a route lazy loaded?
Tips
- Mention `withComponentInputBinding`. It shows you know the current way to read params.
- Explain the "first match wins" rule using the wildcard route.
- Say that `href` would reload the page, which is the whole reason for `routerLink`.
Evaluation rubric
- Names provideRouter, router-outlet, and routerLink.
- Reads route parameters in a current way.
- Understands route order and pathMatch.