Angular Can Activate Guard



    Jose Romero

    Angular TypeScript Recipes

    This code defines a class, NameGuard, that implements the CanActivate interface. This interface defines a single method, canActivate, which returns a boolean value. This method is called by the ActivatedRouteSnapshot constructor when the NameGuard class is injected into an ActivatedRoute. The RouterStateSnapshot constructor is used to obtain the current state of the router. If the route is activated, the canActivate method returns true. If the route is not activated, the canActivate method returns false.

    Library: angular

    Shortcut: angular.guard.can_activate

    import { Injectable } from '@angular/core';
    import { ActivatedRouteSnapshot, CanActivate, RouterStateSnapshot } from '@angular/router';
    @Injectable({providedIn: 'root'})
    export class NameGuard implements CanActivate {
      constructor() { }
      canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot) {
        return true;
    Codiga Logo
    Codiga Hub
    • Rulesets
    • Playground
    • Snippets
    • Cookbooks
    soc-2 icon

    We are SOC-2 Compliance Certified

    G2 high performer medal

    Codiga – All rights reserved 2022.