bug canLoad WHEN redirect to ["/"] and return false
See original GitHub issueI’m submitting a …
[x] bug report with CanLoad Guard
Current behavior
Actually I lazy load my LoginModule and I have canLoad guard. In this canLoad I just check if token is valid.
- If NOT valid I simply
return true;and it works LoginModule is allowed to load The problem is : - if token IS valid I don’t want to load LoginModule I simply navigate to home route and return false using:
this.router.navigate(['/']);
return false;
Problem is when in canLoad guard you return false the url is automatically cleared and my navigate(['/']); is never fired :S
Expected behavior
Simply execute navigate(['/']); when return false; in canLoad
Minimal reproduction of the problem with instructions can’t reproduce but here’s my routing:
const APP_ROUTES: Routes = [
{ path: 'login', loadChildren: 'app/login/login.module#LoginModule', canLoad: [CanLoadLoginGuard]},
...
{ path: '', component:HomeComponent, pathMatch: 'full', canActivate:[CanActivateHomePageGuard] },
{ path: '**', redirectTo:'' }
];
Please tell us about your environment: ng 4.0.3, ng-cli 1.0.0, ts 2.3 (win10,webstorm,npm)
Update (more infos)
I tried plunker here and can’t reproduce because I need to come for first time to certain route where I have canLoad, afaik in plunker preview you can’t come directly to …/mod1 but only on …/
I have zipped project with very light/simple example. So if you have angular-cli you can simply run npm i&& ng s and open your browser on localhost:4200/mod1 then CANCEL confirm() dialog, that’s all!
Download issue16197.zip project (angular-cli)
Cheers
Issue Analytics
- State:
- Created 6 years ago
- Reactions:3
- Comments:8 (5 by maintainers)

Top Related StackOverflow Question
Here is my understanding of this :
When a canLoad returns false the url is changed, not sure if it’s to the previous or if it’s the partial url of the route we’re trying to reach, but in this case if you try to go to
/mod1and canLoad return false the url is changed to/. It’s just the location url changing and not anavigate(). So then when thenavigate(['/'])kicks in, the router thinks we’re moving from/to/and discard the navigation because it’s the same url.Current workaround
You can create a route specific for that :
And just navigate to it instead of navigating to root :
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.