angular2: how to get Full path on CanLoad guard while maintaining redirect url

If you look the signature of the canLoad method, there is is a second parameter segments which you can use to generate the url.

canLoad(route: Route, segments: UrlSegment[]): Observable<boolean> | Promise<boolean> | boolean

You can generate the full path using following code:

canLoad(route: Route, segments: UrlSegment[]): boolean {

  const fullPath = segments.reduce((path, currentSegment) => {
    return `${path}/${currentSegment.path}`;
  }, '');

  console.log(fullPath);

}

Note: With this method you will get the full path, but you will not get any query parameters.


There is another temporary workaround instead of replacing canLoad with canActivate:

You can store the url via redirectUrl = location.pathname and redirect later via this.router.navigateByUrl(redirectUrl);. Of course, if you work on a subpath (like domain) you have to adjust the pathname a little bit.


I have this exact same problem. These issues are related

  • https://github.com/angular/angular/issues/17359
  • https://github.com/angular/angular/issues/12411

And they even have an open PR about this which hasn't been merged yet

  • https://github.com/angular/angular/pull/13127

The workaround for now seems to be to replace the canLoad method with the canActivate, there you can have access to the full path, the bad thing of course is that you're loading the module

Edit: Also for you Assignment routing, there's no need to call canActivate for each child route. Have it defined on the parent route should apply the guard for all child routes.