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
This issue appears to be more nuanced then I originally thought. Before posting, I had noticed that the injected ActivatedRoute appeared to be the root route, so I tried looping through the ActivatedRoute and then using the result for relative navigation, like so
This didn't work however, so I created this issue.
Just fooling around now though, I see this DOES work when navigating from the /parent/sibling route to the /parent/child route. It DOES NOT work when navigating from /parent to /parent/child.
The issue seems to be that the currentActivatedRoute is being injected and used (which makes sense, seeing as it is the currently activated route), and there isn't a way to get what will become the active route in an ActivatedRoute format. (the next route is only provided as an ActivatedRouteSnapshot, which can't be used for relative navigation).
Having the same issue. I need to relatively route from what the route will be, not from the current ActivatedRoute. Didn't find a workaround yet except from fiddling with the raw url.
The first argument that I pass in is state.url because that's the route I want to go to. The returned URL can then be used with this.router.navigateByUrl(parseUrl(state.url, '../sibling'));.
I might be missing some edge cases but it works for me, so thought I could share it.
Reacted by John, Kevin Schneider, Alexander Takchidi, TheILPlace, Jameson Aranda, Jamie Perkins, Mantu Nigam, Agustin Barrios, Martin Gafert, PS and 8 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
jasonaden commentedon Mar 16, 2018
Looks related to #17023
jorroll commentedon Mar 17, 2018
This issue appears to be more nuanced then I originally thought. Before posting, I had noticed that the injected
ActivatedRouteappeared to be the root route, so I tried looping through theActivatedRouteand then using the result for relative navigation, like soThis didn't work however, so I created this issue.
Just fooling around now though, I see this DOES work when navigating from the
/parent/siblingroute to the/parent/childroute. It DOES NOT work when navigating from/parentto/parent/child.jorroll commentedon Mar 17, 2018
The issue seems to be that the current
ActivatedRouteis being injected and used (which makes sense, seeing as it is the currently activated route), and there isn't a way to get what will become the active route in anActivatedRouteformat. (the next route is only provided as anActivatedRouteSnapshot, which can't be used for relative navigation).SamVerschueren commentedon Apr 6, 2018
Having the same issue. I need to relatively route from what the route will be, not from the current
ActivatedRoute. Didn't find a workaround yet except from fiddling with the raw url.jorroll commentedon Apr 6, 2018
Ya, my current workaround is to fiddle with the raw url.
SamVerschueren commentedon Apr 9, 2018
I wrote this small utility function that I use in my guard
You can call it the following ways
The first argument that I pass in is
state.urlbecause that's the route I want to go to. The returned URL can then be used withthis.router.navigateByUrl(parseUrl(state.url, '../sibling'));.I might be missing some edge cases but it works for me, so thought I could share it.
mantuonweb commentedon Apr 26, 2019
I am facing the same issue relative routing does not working in guard services?
Guys did you get any fix
jorroll commentedon Apr 26, 2019
@mantuonweb you didn't actually read this thread did you?
23 remaining items
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.