What are Route Guards in Angular

๐Ÿ’ก Concept Name

Route Guards โ€“ Features in Angular that determine whether or not navigation to or from a route should be allowed, enabling control over route access.

๐Ÿ“˜ Quick Intro

Angular route guards allow you to protect routes by checking conditions before navigation. You can prevent unauthorized access, ask for confirmation before leaving a page, or block module loading based on user roles.

๐Ÿง  Analogy / Short Story

Think of guards like security checks at doors: only people with valid ID can enter (CanActivate), and people with unfinished tasks are warned before exiting (CanDeactivate). Guards ensure the "doors" of your app open only to the right people at the right time.

๐Ÿ”ง Technical Explanation

  • ๐Ÿ” CanActivate: Checks if a user can access a route.
  • ๐Ÿšซ CanDeactivate: Checks if it's safe to leave a route.
  • ๐Ÿ“ฆ CanLoad: Prevents loading lazy-loaded modules unless conditions are met.
  • ๐Ÿ‘ถ CanActivateChild: Checks child route access.
  • ๐Ÿ“ฅ Resolve: Fetches data before activating a route.
  • ๐Ÿ“ˆ Guards return boolean | Observable<boolean> | Promise<boolean>.

๐ŸŽฏ Purpose & Use Case

  • โœ… Block unauthorized users from accessing secure routes.
  • โœ… Prevent navigation if forms are unsaved.
  • โœ… Control lazy loading of modules (e.g., admin modules).
  • โœ… Secure child routes separately from parent routes.

๐Ÿ’ป Real Code Example

// CanActivate example
import { Injectable } from '@angular/core';
import { CanActivate, Router } from '@angular/router';

                    @Injectable({ providedIn: 'root' })
export class AuthGuard implements CanActivate {
  constructor(private router: Router) { }

  canActivate(): boolean {
    const isLoggedIn = true; // Simulated condition
    if (!isLoggedIn) {
      this.router.navigate(['/login']);
      return false;
    }
    return true;
  }
}

โ“ Interview Q&A

Q1: What are route guards in Angular?
A: Interfaces used to control navigation and access to routes.

Q2: Which guard prevents unauthorized users from accessing a route?
A: CanActivate.

Q3: What is CanDeactivate used for?
A: To confirm if it's safe to leave a route (e.g., unsaved form).

Q4: What does CanLoad prevent?
A: Prevents loading of lazy-loaded modules unless conditions are met.

Q5: Where are guards configured?
A: In route configuration within the routing module.

๐Ÿ“ MCQs

Q1. What does CanActivate check?

  • If a route can be accessed
  • If component is rendered
  • If data is saved
  • If user is logged out

Q2. Which guard confirms before leaving a route?

  • CanLoad
  • Resolve
  • CanDeactivate
  • CanEnter

Q3. Which guard blocks module loading?

  • CanDeactivate
  • CanLoad
  • CanActivate
  • Resolve

Q4. Which Angular object defines route guards?

  • AppComponent
  • Router config
  • Main.ts
  • Polyfill.ts

Q5. What is returned by a guard?

  • Component
  • Boolean or Observable/Promise
  • String
  • null

Q6. Which guard is for child routes?

  • CanChild
  • CanActivateChild
  • CanLoad
  • CanAccess

Q7. What does Resolve do?

  • Clears cache
  • Fetches data before activating route
  • Guards modules
  • Loads CSS

Q8. Where is a guard implemented?

  • In main.ts
  • In service class
  • In component.ts
  • In HTML

Q9. Which guard is best for protecting login routes?

  • Resolve
  • CanDeactivate
  • CanLoad
  • CanActivate

Q10. What happens if CanActivate returns false?

  • Navigation continues
  • Component resets
  • Route reloads
  • Navigation is blocked

๐Ÿ’ก Bonus Insight

Route guards help enforce clean UX and application security. Combine them with role-based auth systems to ensure only the right users can see specific content and perform actions in Angular apps.

๐Ÿ“„ PDF Download

Need a handy summary for your notes? Download this topic as a PDF!

Learn More About Angular ๐Ÿš€

What is Angular vs AngularJS ๐Ÿ‘‰ Explained
Key Features of Angular ๐Ÿ‘‰ Explained
Angular Architecture ๐Ÿ‘‰ Explained
Components in Angular ๐Ÿ‘‰ Explained
Modules in Angular ๐Ÿ‘‰ Explained
Templates in Angular ๐Ÿ‘‰ Explained
Directives in Angular ๐Ÿ‘‰ Explained
Structural vs Attribute Directives ๐Ÿ‘‰ Explained
Services in Angular ๐Ÿ‘‰ Explained
Dependency Injection in Angular ๐Ÿ‘‰ Explained
Data Binding in Angular ๐Ÿ‘‰ Explained
Interpolation ๐Ÿ‘‰ Explained
Property Binding ๐Ÿ‘‰ Explained
Event Binding ๐Ÿ‘‰ Explained
Two-way Data Binding ๐Ÿ‘‰ Explained
ngModel Usage ๐Ÿ‘‰ Explained
Lifecycle Hooks ๐Ÿ‘‰ Explained
ngOnInit Hook ๐Ÿ‘‰ Explained
ngOnDestroy Hook ๐Ÿ‘‰ Explained
Pipes in Angular ๐Ÿ‘‰ Explained
Pure vs Impure Pipes ๐Ÿ‘‰ Explained
Create Custom Pipe ๐Ÿ‘‰ Explained
Angular Forms ๐Ÿ‘‰ Explained
Template-driven vs Reactive Forms ๐Ÿ‘‰ Explained
Form Controls ๐Ÿ‘‰ Explained
Form Validation ๐Ÿ‘‰ Explained
Validation Error Messages ๐Ÿ‘‰ Explained
FormGroup vs FormControl ๐Ÿ‘‰ Explained
Enable/Disable Form Fields ๐Ÿ‘‰ Explained
Reset Form ๐Ÿ‘‰ Explained
Routing in Angular ๐Ÿ‘‰ Explained
RouterModule ๐Ÿ‘‰ Explained
Configure Routes ๐Ÿ‘‰ Explained
Route Guards ๐Ÿ‘‰ Explained
CanActivate vs CanDeactivate ๐Ÿ‘‰ Explained
Lazy Loading ๐Ÿ‘‰ Explained
Preloading in Routing ๐Ÿ‘‰ Explained
Wildcard Routes ๐Ÿ‘‰ Explained
Route Parameters ๐Ÿ‘‰ Explained
Query Parameters ๐Ÿ‘‰ Explained
Programmatic Navigation ๐Ÿ‘‰ Explained
routerLink & routerLinkActive ๐Ÿ‘‰ Explained
Resolvers in Routing ๐Ÿ‘‰ Explained
Observables in Angular ๐Ÿ‘‰ Explained
RxJS in Angular ๐Ÿ‘‰ Explained
Observable vs Promise ๐Ÿ‘‰ Explained
Share:

Tags:


Feedback Modal Popup