your recipe card header background

    Angular CanDeactivate Guard

    0

    0

    lucycodes42

    Angular snippets for TypeScript

    @Injectable({providedIn: 'root'}) export class ComponentNameComponent implements CanDeactivate<ComponentNameComponent> { canDeactivate( component: ComponentNameComponent, currentRoute: ActivatedRouteSnapshot, currentState: RouterStateSnapshot ): Observable<boolean> { return this.props.canDeactivate; } }

    In this code, the @Injectable() decorator is used to declare that the NameGuard and CanDeactivate classes are injectable. The @Injectable() decorator also declares that the NameGuard and CanDeactivate classes will be able to access the canDeactivate property of the ComponentNameComponent class. The canDeactivate property is a property of the ComponentNameComponent class that returns an Observable object, a Promise object, or a boolean value. The code also declares that the NameGuard and CanDeactivate classes will be able to access the currentRoute, currentState, and canDeactivate properties of the ActivatedRouteSnapshot and RouterStateSnapshot classes. The canDeactivate property of the ActivatedRouteSnapshot and RouterStateSnapshot classes is

    Library: angular

    Shortcut: a_guard_can_deactivate

    import { Injectable } from '@angular/core';
    import { ActivatedRouteSnapshot, CanDeactivate, RouterStateSnapshot } from '@angular/router';
    import { Observable } from 'rxjs';
    
    import { ComponentNameComponent } from './filename.component';
    
    // Consider using this interface for all CanDeactivate guards,
    // and have your components implement this interface, too.
    //
    //   e.g. export class CanDeactivateGuard implements CanDeactivate<CanComponentDeactivate> {
    //
    // export interface CanComponentDeactivate {
    // canDeactivate: () => any;
    // }
    
    @Injectable({providedIn: 'root'})
    export class NameGuard implements CanDeactivate<ComponentNameComponent> {
      canDeactivate(
        component: ComponentNameComponent,
        currentRoute: ActivatedRouteSnapshot, 
        currentState: RouterStateSnapshot
      ): Observable<boolean>|Promise<boolean>|boolean {
        return false;
      }
    }
    Codiga Logo
    Codiga Hub
    • Rulesets
    • Explore
    • Cookbooks
    • Playground
    soc-2 icon

    We are SOC-2 Compliance Certified

    G2 high performer medal

    Codiga – All rights reserved 2022.