Saturday, March 14, 2020

How to get the name of the route the user try to navigate on canAcitvate method?

when we want to know what component the use try to access with AuthGuard enable, it is quit simple

we can put a console.log in the canActivate Method, to monitor the ActivateRouteSnapshot in Angular 9


console.log("the name of route: " + destination.routeConfig.component.name);
 

in Angular 6

you can access it from route.component.name

here is the sample code


import { Injectable } from "@angular/core";
import { CanActivateRouterActivatedRouteSnapshot 
RouterStateSnapshot } from '@angular/router';

@Injectable()
export class longinGuard implements CanActivate{
    constructor(private routerRouter){

    }
    canActivate(destinationActivatedRouteSnapshotstateRouterStateSnapshot){
        console.log("the name of route navigate to: " +
  destination.routeConfig.component.name);
  
    }
}

No comments:

Post a Comment