Route guards let you control whether navigation to (or away from) a route is allowed, protecting authenticated pages or confirming unsaved changes. This lesson covers the main guard types using the modern functional approach.
What Is a Route Guard?
A route guard is a function (or, in classic Angular, a class) that Angular's router calls before activating, deactivating, or matching a route. Returning true allows the navigation to proceed; returning false (or a UrlTree redirect) blocks it.
Modern Angular favors functional guards, plain functions using inject(), over class-based guards implementing an interface, since they involve less boilerplate.
Returning a UrlTree (via createUrlTree) redirects the user instead of just blocking navigation with false.
Guard Types
canActivate: [authGuard] // can this route be entered?
canActivateChild: [childGuard] // can child routes under this one be entered?
canDeactivate: [unsavedGuard] // can we leave the current route?
canMatch: [featureFlagGuard] // should this route even be considered a match?
canActivate runs before a route is entered; returning false/redirect cancels navigation.
canDeactivate runs before leaving a route, useful for "unsaved changes" confirmation prompts.
canMatch decides whether a route definition should be considered a match at all, useful for feature flags.
All guard types support returning a boolean, a UrlTree, or an Observable/Promise resolving to either.
Route Guards Cheatsheet
The functional guard types and what each one controls.
Guard
Question It Answers
canActivate
Can the user enter this specific route?
canActivateChild
Can the user enter any child route under this one?
canDeactivate
Can the user leave the current route right now?
canMatch
Should this route definition even be considered a match?
Return true
Allow the navigation
Return false
Block the navigation silently
Return UrlTree
Redirect somewhere else instead of blocking
Protecting Routes With canActivate
The most common guard use case is blocking access to authenticated-only routes and redirecting anonymous users to a login page, optionally remembering where they were trying to go.
canDeactivate guards run before the router leaves the currently active route, a natural place to warn users about unsaved form changes before they navigate away accidentally.
canMatch runs before the router even considers a route a candidate match, useful for feature-flagged routes: if the guard returns false, the router tries the next matching route definition (like a fallback) instead of just blocking navigation.
Modern Angular favors functional guards using inject() over class-based guard implementations.
Guards can return a boolean or a UrlTree to redirect instead of just blocking navigation.
Pro Tip
When blocking navigation, prefer returning a UrlTree (via router.createUrlTree()) over a plain false, it gives users a clear next step (like a login page) instead of leaving them stuck with no explanation.
You now understand route guards. Next, move into Angular Forms to learn how to build and validate user input.