Guard Can Deactivate



    Jose Romero

    Angular TypeScript Recipes

    The NameGuard class implements the CanDeactivate interface, which specifies that the class can be used to determine if a component can be deactivated. The code first imports the CanDeactivate interface and then defines a method called canDeactivate. This method takes three arguments: the current route object, the current state object, and the ComponentNameComponent object.

    The canDeactivate method returns an Observable that will tell you if the component can be deactivated. If the component can be deactivated, the canDeactivate method will return true. Otherwise, the canDeactivate method will return false.

    Library: angular

    Shortcut: angular.guard.deactivate

    import { Injectable } from '@angular/core';
    import { ActivatedRouteSnapshot, CanDeactivate, RouterStateSnapshot } from '@angular/router';
    import { Observable } from 'rxjs';
    @Injectable({providedIn: 'root'})
    export class NameGuard implements CanDeactivate<ComponentNameComponent> {
        component: ComponentNameComponent,
        currentRoute: ActivatedRouteSnapshot, 
        currentState: RouterStateSnapshot
      ): Observable<boolean>|Promise<boolean>|boolean {
        return false;
    Codiga Logo
    Codiga Hub
    • Rulesets
    • Playground
    • Snippets
    • Cookbooks
    • Security
    • Privacy Policy
    • Code Privacy
    • Terms of Service
    soc-2 icon

    We are SOC-2 Compliance Certified

    G2 high performer medal

    Codiga – All rights reserved 2022.