Angular: Router: Current route unavailable when calling CanActivate guard during initial page load

Created on 19 Sep 2016  路  3Comments  路  Source: angular/angular

I'm submitting a ... (check one with "x")

[ X ] bug report => search github for a similar issue or PR before submitting
[ ] feature request
[ ] support request => Please do not submit support request here, instead see https://github.com/angular/angular/blob/master/CONTRIBUTING.md#question

Current behavior
No provider error when attempting to use _ActivatedRouteSnapshot_ in a guard applied to initially loaded URL (_CanActivate_ function called during initial page load). _Router_ object is accessible but state snapshot url property is an empty string.

Expected behavior
No error, URL/route information available regardless of when _CanActivate_ is called.

Reproduction of the problem
http://plnkr.co/edit/aJpxcpvperSM0qzcvd4G

Define a route in main app module with a _CanActivate_ guard service with _ActivatedRouteSnapshot_ in constructor and try to load that route initially. Workaround: subscribe to router events in main app constructor and save initial URL; reference this string in the _CanActivate_ if current router state snapshot url is blank.

What is the motivation / use case for changing the behavior?
Apply route guard to initial URL

Please tell us about your environment:
SystemJS loader

  • Angular version: 2.0.0 , @angular/router 3.0.0
  • Browser: Chrome
  • Language: Typescript 1.8.10
  • Node (for AoT issues): node --version =
router

Most helpful comment

The ActivatedRouteSnapshot and RouterStateSnapshot are not injected. They are arguments in the canActivate method.

You can use the RouterStateSnapshot.url to get the intended destination URL

http://plnkr.co/edit/Wj0mRchDCUxGAdipPsuW?p=preview

All 3 comments

The ActivatedRouteSnapshot and RouterStateSnapshot are not injected. They are arguments in the canActivate method.

You can use the RouterStateSnapshot.url to get the intended destination URL

http://plnkr.co/edit/Wj0mRchDCUxGAdipPsuW?p=preview

@brandonroberts

Geez, don't know how I messed this up, thanks for pointing me in the right direction!

This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

_This action has been performed automatically by a bot._

Was this page helpful?
0 / 5 - 0 ratings