You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
bug: relative navigation doesn't work inside route guards #22763
Inside a route guard, injecting ActivatedRoute and attempting relative navigation via this.router.navigate(['../sibling'], {relativeTo: this.route}) results in a Error: Cannot match any routes. URL Segment: 'sibling'
Expected behavior
Relative navigation should work normally
Minimal reproduction of the problem with instructions
What is the motivation / use case for changing the behavior?
This appears to be a bug
If a user navigates to a child route and includes a specific queryParam, I'd like to redirect the user to a sibling route. Currently, this does not appear to be possible inside a route guard with relative navigation.
Additional Information
It appears that the ActivatedRoute injected into a route guard is always an instance of the root route. Inside a route guard, this.router.navigate(['.'], {relativeTo: this.route}) always takes the user back to the root route, rather than navigating them to the same route they are currently on.
Environment
Angular version: 5.0.0
Browser:
- [x] Chrome (desktop) version 64
For me the "expected" solution would be to accept a RouterStateSnapshot in Router.createUrlTree. From what I can tell snapshot property is the only thing from the route that is used anyway:
Still having this Issue in Angular 9 ... It's a pity, that there is no priority in fixing this.
Reacted by jmaelstafbetclic, Wilt, Jake Sims, Eyal Ellenbogen, Kevin Schneider, Pierre-Luc Champigny, Alisher Nuritdinov, Victor Pishuk, Andreas Loew, Mereo4 and 16 more
Reacted by Federico Dainelli, D. St-Jacques, Stefan Mototolea, Alex Hoisan, András Kovács, Dion Jansen, Ivan Soroka, Sébastien Gérard, Henry Martinez, Hrvoje Husnjak and 21 more
The TL;DR for this one is that this is something I would absolutely love to do but will be a breaking change to update router.createUrlTree to do it.
The implementation for creating a URL tree is really confusing and actually requires that the ActivatedRoute/ActivatedRouteSnapshot be part of the current router url. In reality, we should really be able to create a url from any ActivatedRouteSnapshot, but that requires the shape of the ActivatedRoute/ActivatedRouteSnapshot to be valid (which is commonly not the case in tests due to stubbing this out).
There is maybe an opportunity to introduce this new way of creating the URL as a standalone function exported as public API in the router and then later making the breaking change to include it in router.createUrlTree. This would be problematic for discovery and documentation but would at least provide an early opt-in for anyone trying to do this.
Activity
mantuonweb commentedon Apr 26, 2019
Yes , I did but may be this fixed in updated one.
Just wants to know that
MarcusRiemer commentedon Feb 25, 2020
It really is a pity that this is so hard. Another workaround was mentioned in #28682, but this also doesn't seem to work anymore with Angular 9:
For me the "expected" solution would be to accept a
RouterStateSnapshotinRouter.createUrlTree. From what I can tellsnapshotproperty is the only thing from the route that is used anyway:angular/packages/router/src/create_url_tree.ts
Lines 145 to 158 in 5edeee6
Called from
angular/packages/router/src/create_url_tree.ts
Line 27 in 5edeee6
Which in turn is called from https://github.com/angular/angular/blob/9.0.2/packages/router/src/router.ts#L951
@jasonaden (or anyone from the Angular team): This seems like a trivial fix, so I guess im am overlooking something?
NLueg commentedon Mar 12, 2020
Still having this Issue in Angular 9 ... It's a pity, that there is no priority in fixing this.
ddralves commentedon Nov 17, 2020
Any update on this issue?
sergiypayu commentedon Feb 15, 2022
Here is my workaround:
atscott commentedon Apr 22, 2022
The TL;DR for this one is that this is something I would absolutely love to do but will be a breaking change to update
router.createUrlTreeto do it.The implementation for creating a URL tree is really confusing and actually requires that the
ActivatedRoute/ActivatedRouteSnapshotbe part of the current router url. In reality, we should really be able to create a url from anyActivatedRouteSnapshot, but that requires the shape of theActivatedRoute/ActivatedRouteSnapshotto be valid (which is commonly not the case in tests due to stubbing this out).There is maybe an opportunity to introduce this new way of creating the URL as a standalone function exported as public API in the
routerand then later making the breaking change to include it inrouter.createUrlTree. This would be problematic for discovery and documentation but would at least provide an early opt-in for anyone trying to do this.angular-automatic-lock-bot commentedon Jun 24, 2022
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.