Thursday, June 7, 2018

Look at the routing events in an Angular 4 application.

ngOnInit() {
   this.whereAreWeReally = this.activatedRoute.snapshot.url[0] ?
         this.activatedRoute.snapshot.url[0].path : '' ;
   this.router.events.subscribe((event:any) => {
      if(event.toString().split("(")[0] === 'NavigationCancel'){
         this.isNavigationCanceled = true;
      }
      if(event.url) {
         const routePieces = event.url.split("/");
         this.whereAreWeMaybe = routePieces[routePieces.length - 1];
      }
      if(event.route) {
         this.whereAreWeMaybe = event.route.path;
      }
   });
}

 
 

Routing events may vary. NavigationStart, RoutesRecognized, GuardsCheckStart, GuardsCheckEnd, ResolveStart, ResolveEnd, and NavigationEnd should come in said order and if NavigationCancel appears the last three of these will not appear.

No comments:

Post a Comment