October DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsPC HealthRecommendedCrashes, freezes, slowdowns? Check your PC nowSpot repairable issues before they interrupt work.Check PCOctober DealsAmazon USDeal season is back - check today's better picksAmazon US: current deals, useful picks and tech finds.See Picks×
Skip to content
World desk3 min

Angular Route Guards: Choose the Right Guard and Return the Right Result

Choose an Angular route guard by the navigation decision you need: route entry, child entry, leaving a route, or matching. Learn how false results, redirects, testing, and server-side authorization work.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Angular route guards control what happens during navigation: whether a route can activate, whether a child route can activate, whether the current route can be left, or whether a route configuration can match. Choose the guard for the decision you need, return a router-recognized result, and enforce authorization for protected data on the server as well.

Which Angular route guard should you use?

These guards address different moments in navigation; they are not interchangeable.

As an Amazon Associate I earn from qualifying purchases.

Guard When it runs What a false result means Typical use
CanActivate Before a route activates Navigation is canceled Check whether a user may enter a route
CanActivateChild Before a child route activates Navigation to that child is canceled Apply a shared check to routes nested under a parent
CanDeactivate Before leaving the active route Navigation away is canceled Ask whether to leave a page with unsaved changes
CanMatch While Angular evaluates whether a route configuration matches Angular skips that route and tries other configurations Select a route conditionally or provide a fallback

The distinction matters most for CanMatch: returning false does not simply deny navigation. Another route configuration may still match the same URL. By contrast, a false result from the activation or deactivation guards cancels that navigation.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

How do you add a guard to a route?

Angular supports functional guards and route-level dependency injection. A guard can use application services to check authentication, permissions, or feature state. Attach it in the route configuration, for example:

{ path: 'account', component: AccountComponent, canActivate: [authGuard] }

Multiple guards can be listed in an array; Angular runs them in the order listed. For child-route checks, put canActivateChild on the parent route whose children need protection, and verify the route nesting: the parent URL itself is distinct from its child routes.

Guards can return a boolean, a UrlTree, a RedirectCommand, or a Promise or Observable that resolves to one of those results. For an asynchronous return, the router uses the first emitted value and unsubscribes.

How should a guard redirect?

Return a UrlTree or RedirectCommand when the guard should send the user elsewhere. This lets the router handle the redirect as part of navigation. Angular advises against returning false and then calling navigation imperatively from inside the guard.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Keep redirect configuration separate from guard decisions. Angular uses prefix matching for redirects by default. If an empty-path redirect should apply only to the root, set pathMatch: 'full'; otherwise, the prefix rule may make it apply to other paths too.

How does CanMatch fall-through work?

Angular evaluates route configurations in order and uses the first match. If a CanMatch guard returns false, Angular skips that candidate and continues matching. This supports deliberate conditional routing, such as offering a later route with the same path as an alternate component or fallback.

Put more specific paths before less specific ones, and keep wildcard fallbacks last. Together, route order and the guard result determine which configuration Angular considers next.

Are Angular route guards a security boundary?

No. A guard can shape the navigation experience and keep a route out of the interface, but it cannot secure an API or its data. Browser JavaScript can be modified by the person using the browser. Angular’s routing guide states: “Always enforce user authorization server-side, in addition to any client-side guards.” The server must independently authorize protected requests and data.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

How should you test route guards?

Test what the router does, not only what a guard function returns. Angular’s routing testing guide demonstrates mocking dependencies and using RouterTestingHarness to exercise routed components and navigation.

  • Verify that an allowed navigation reaches the expected route.
  • Verify that a denied activation or deactivation cancels navigation.
  • Verify that a returned redirect takes the user to the intended destination.
  • For CanMatch, verify that a false result falls through to the intended later route, if one exists.

What about CanLoad?

The current Angular Route API marks canLoad as deprecated and directs developers to use canMatch instead. For new route-matching decisions, use CanMatch rather than treating canLoad as the preferred current approach.

Angular documentation

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Leave a Reply

Your email address will not be published. Required fields are marked *

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
PC Slower Than It Used to Be?Free scan - under a minute
Outdated Drivers Are Slowing You DownFree scan - exact matches

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.