Skip to main content

Route Guards

Route guards are functions or classes that control navigation in Angular applications. They can prevent navigation, redirect to different routes, or allow navigation to proceed based on custom logic.

Import

Guard Types

Angular provides several types of guards for different navigation scenarios:

CanActivate

Controls if a route can be activated

CanActivateChild

Controls if child routes can be activated

CanDeactivate

Controls if a route can be deactivated

CanMatch

Controls if a route can be matched
Modern Angular applications use functional guards (functions) instead of class-based guards. Functional guards are more concise and leverage dependency injection through the inject() function.

Guard Result Types

GuardResult

All guards return a GuardResult, which can be:
  • true - Allow navigation to proceed
  • false - Cancel navigation
  • UrlTree - Redirect to a different URL
  • RedirectCommand - Redirect with navigation options

MaybeAsync

Guards can return synchronous values or async values (Observable or Promise).

CanActivate

CanActivateFn

Determines if a route can be activated. Used to protect routes that require authentication, authorization, or other preconditions. Parameters:
ActivatedRouteSnapshot
required
The activated route snapshot containing route parameters, data, and configuration.
RouterStateSnapshot
required
The router state snapshot containing the target URL and routing tree.
Returns: true to allow, false to cancel, or UrlTree/RedirectCommand to redirect. Examples:
Route Configuration:

CanActivate Interface (Deprecated)

Class-based guards using CanActivate interface are deprecated. Use functional guards (CanActivateFn) instead.

CanActivateChild

CanActivateChildFn

Determines if child routes can be activated. Applied to parent routes to protect all child routes. Example:
Route Configuration:

CanDeactivate

CanDeactivateFn

Determines if a route can be deactivated. Commonly used to prevent navigation away from pages with unsaved changes. Parameters:
T
required
The component instance being deactivated.
ActivatedRouteSnapshot
required
The current activated route.
RouterStateSnapshot
required
The current router state.
RouterStateSnapshot
required
The target router state.
Examples:
Component Implementation:
Route Configuration:

CanMatch

CanMatchFn

Determines if a route configuration can be matched. Used for feature flags, conditional routing, and A/B testing. Parameters:
Route
required
The route configuration being evaluated.
UrlSegment[]
required
The URL segments that have not been consumed yet.
PartialMatchRouteSnapshot
The current route snapshot up to this point in matching (optional for backward compatibility).
Examples:
Route Configuration:
Unlike CanActivate, CanMatch prevents the route from being matched at all. If a canMatch guard returns false, the router continues to the next route configuration. This is ideal for feature flags and conditional routing.

CanLoad (Deprecated)

CanLoadFn

CanLoad is deprecated in favor of CanMatch. Use CanMatchFn for all new code.

RedirectCommand

RedirectCommand Class

Used in guards and resolvers to redirect with specific navigation options. Example:

Guard Execution Order

When multiple guards are present, they execute in the following order:
  1. Route matching: canMatch guards
  2. Activation: canActivate guards (from parent to child)
  3. Child activation: canActivateChild guards (from parent to child)
  4. Deactivation: canDeactivate guards (when leaving route)
Example:

Common Guard Patterns

Combining Multiple Guards

Reusable Permission Guard

Guard with Loading State

Guard with Error Handling

Testing Guards

See Also

Router Class

Router service API

Route Configuration

Configure routes with guards

Route Guards Guide

Complete guide to route guards

Dependency Injection

Using inject() in guards