Routing
Routing was a separate library install in React (React Router, Intermediate Chapter 5). In Angular it's part of the framework — the same single-page navigation concepts apply directly, just with Angular's own API. This chapter covers defining routes, linking and navigating, reading URL parameters, and protecting routes with guards.
Defining Routes
Routes are an array of objects mapping a path to a component — the direct equivalent of React Router's <Route> elements. :id marks a dynamic segment (matching /product/anything), and '**' is the catch-all for any unmatched URL — both familiar from React Router, just expressed as data rather than JSX. This array is wired into the app's configuration once, in main.ts, via provideRouter(routes).
RouterOutlet and RouterLink
<router-outlet> is the placeholder where the matched route's component renders — the equivalent of React Router's <Outlet /> (or the <Routes> block itself). routerLink="/about" navigates without a full page reload, exactly like React's <Link to> — and the same warning applies: a plain <a href> would trigger a real reload and defeat the point.
Reading Route Parameters
ActivatedRoute — injected like any service (Chapter 6) — exposes the current route's parameters. snapshot.paramMap.get('id') reads the :id segment from the URL, the equivalent of React Router's useParams(). The snapshot form is the simplest; for a route that the same component stays on while only the parameter changes, subscribing to route.paramMap (an Observable, Chapter 11) reacts to those changes — but snapshot suffices for most cases.
Programmatic Navigation
Injecting the Router service gives navigate([...]) for navigating from code — after a form submits, a login succeeds, an action completes — the equivalent of React Router's useNavigate(). The path is passed as an array of segments, which Angular joins into the URL.
Route Guards — Protecting a Route
A guard is a function that runs before a route activates, returning true to allow it or redirecting otherwise — there's no built-in equivalent in React Router itself (it's typically hand-rolled with a wrapper component). Added to a route via canActivate: [authGuard], this is the standard way to keep an unauthenticated user out of a protected page, redirecting them to login instead. Guards inject services freely, since they run inside Angular's DI context.
lazy/Suspense) directly with routing: { path: 'admin', loadComponent: () => import('./admin.component').then(m => m.AdminComponent) } loads that component's code only when its route is first visited. No separate Suspense boundary is needed — the router handles the loading transition itself.
'**' route must be the last entry in the array — placed earlier, it would match everything and prevent any route below it from ever being reached. The same ordering discipline applied to React Router's path="*".
| Angular | React Router equivalent |
|---|---|
| routes array + provideRouter | <Routes> / <Route> |
| <router-outlet> | <Outlet /> |
| routerLink="/x" | <Link to="/x"> |
| ActivatedRoute paramMap | useParams() |
| Router.navigate([...]) | useNavigate() |
| canActivate guard | Hand-rolled protected-route wrapper |
Coding Challenges
Set up a 3-page app (Home, About, Contact) with a routes array, a nav using routerLink, a router-outlet, and a wildcard route showing a NotFound component for unmatched URLs.
📄 View solutionAdd a product/:id route. From a product list, use routerLink (or Router.navigate) to go to a detail page that reads the id via ActivatedRoute and displays the matching product.
📄 View solutionBuild an authGuard (CanActivateFn) backed by a simple AuthService with an isLoggedIn flag, protecting a /dashboard route — redirecting to /login when not logged in — and a button toggling the logged-in state to test both outcomes.
📄 View solutionChapter 10 Quick Reference
- routes array (path → component), wired via
provideRouter(routes)— built into Angular - <router-outlet> renders the matched route; routerLink navigates without reload
- :id dynamic segments read via
ActivatedRoute.snapshot.paramMap.get('id') - Router.navigate([...]) — programmatic navigation from code
- canActivate: [guard] — a function gating a route, allowing or redirecting
- loadComponent in a route — lazy-loads that component's code on first visit
- The
'**'wildcard route must be last; matching is top-to-bottom - Next chapter: HTTP client and RxJS — talking to a backend with Observables