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> {
        canDeactivate(
          component: ComponentNameComponent,
          currentRoute: ActivatedRouteSnapshot, 
          currentState: RouterStateSnapshot
        ): Observable<boolean>|Promise<boolean>|boolean {
          return false;
        }
      }
      Typescript language logo
      your recipe card header background

      Guard Can Deactivate

      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

      0 Comments

        Add Comment

        Log in to add a comment

        Codiga - All rights reserved 2022.