Showing posts with label Router Guards. Show all posts

How many route guards are available in Angular 17?

Angular 17 Router Guards: A Concise Guide

In Angular 17, router guards have evolved to provide a more concise and modern approach, leveraging functional programming introduced in Angular v14.2. These guards facilitate fine-grained control over navigation, authentication, data resolution, and lazy loading of modules. Let's explore the six different router guards available in Angular 17:

1. CanActivateFn

  • Purpose: Determines if a route can be activated.
  • Logic: Returns a boolean, UrlTree, or an Observable/Promise resolving to one of these.
    • true: Navigation proceeds.
    • false: Navigation is blocked.
    • UrlTree: Redirection to a different route.
  • Use Cases:
    • Authentication checks (redirect to login if not authorized)
    • Role-based authorization
    • Prevent access to partially completed forms