What you want to use is the RouterStateSnapshot parameter of your guard's RouterStateSnapshot method.
RouterStateSnapshot has the current path that you are in the routing process, while Router.url is the current path you are routing from (this is why @ Günter Zöchbauer's answer doesn’t quite work in this scenario).
To make this fully applicable to your use case without additional code, you will want to change your routes to
/account/secret/form and /account/secret/form/terms
import { Injectable } from '@angular/core'; import { ActivatedRouteSnapshot, CanActivate, Router, RouterStateSnapshot } from '@angular/router'; import { Observable } from 'rxjs/Observable'; @Injectable() export class ChildRouteGuard implements CanActivate { constructor(private router: Router) { } canActivate(_route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> | Promise<boolean> | boolean { if (
Change 1
You can save your current URL structure by also using ActivatedRouteSnapshot , as shown below
Assuming routes like account/secret/form and account/secret/terms
route.url is an array of URL segments, we want the latter because it will be a form . We find the index of this in the current route, which we are trying to switch to account/secret/form and slice . At this point, we have a “parent” current route to which we are trying to switch to account/secret , and we can add in sibling to the form , which is the terms .
Note: this pattern breaks up when you have duplicate occurrences of form in your URL
import { Injectable } from '@angular/core'; import { ActivatedRouteSnapshot, CanActivate, Router, RouterStateSnapshot } from '@angular/router'; import { Observable } from 'rxjs/Observable'; @Injectable() export class ChildRouteGuard implements CanActivate { constructor(private router: Router) { } canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> | Promise<boolean> | boolean { if (