Repository navigation
Closed
Description
I'm submitting a ...
[ ] Regression (behavior that used to work and stopped working in a new release)
[x] Bug report
[ ] Feature request
[ ] Documentation issue or request
[ ] Support request => Please do not submit support request here, instead see https://github.com/angular/angular/blob/master/CONTRIBUTING.md#question
Current behavior
Given the following routing:
- Route
'parent'- Route
'child'- Route
''with componentTestComponent
- Route
- Route
If the component TestComponent tries to programmatically navigate to itself with some parameters (e.g. /parent/child -> /parent/child;foo=123) using relativeTo, the navigation fails with the following error:
this.router.navigate(['.', {foo: 123}], {relativeTo: this.route});
ERROR TypeError: Cannot read property 'path' of undefined
at createNewSegmentGroup (create_url_tree.ts:290)
at updateSegmentGroup (create_url_tree.ts:295)
at createUrlTree (create_url_tree.ts:47)
at Router.createUrlTree (router.ts:433)
at Router.navigate (router.ts:495)
at TestComponent.navigateRelative (app.ts:38)
at Object.eval [as handleEvent] (TestComponent.html:2)
at handleEvent (view.ts:142)
at callWithDebugContext (services.ts:815)
at Object.debugHandleEvent [as handleEvent] (services.ts:411)
Expected behavior
The navigation should work.
Minimal reproduction of the problem with instructions
Full Working Example
- Open the Plunkr and launch the application in a separate window
- Click on "Navigate Relative"
- Open the browser console and inspect the error
Note: If you click on "Navigate Absolute", which performs an absolute navigation, it works as intended.
Details
- Create the following routing configuration:
export const APP_ROUTES: Routes = [
{
path: 'parent',
children: [
{
path: 'child',
children: [
{
path: '',
pathMatch: 'full',
component: TestComponent
}
]
}
]
}
];
- In
TestComponent, call the methodnavigateRelative:
@Component(....)
export class TestComponent {
constructor(private router: Router, private route: ActivatedRoute) {
}
navigateRelative(): void {
this.router.navigate(['.', {foo: 123}], {relativeTo: this.route});
}
}
- Observe the error described above.
What is the motivation / use case for changing the behavior?
Relative navigation to myself with different parameters should work. If the path is not empty, it works as well.
Please tell us about your environment
Angular version: 4.2.6
Browser: all
Reacted by John, Jon Rimmer and Maksim
Activity
jasonaden commentedon Jul 17, 2017
@oliverhanappi So the basic problem is that you can't cause a navigation to the same URL with different parameters, right? What did you mean by:
oliverhanappi commentedon Jul 18, 2017
@jasonaden Yes, exactly, but only when the currently activated component is defined by a route with an empty path.
If you would change
to the following: (thus switching to a non empty path)
the navigation
parent/child/grandchild -> /parent/child/grandchild;foo=123works as expected using:Here is a Plunkr which demonstrates that it works using non empty paths: http://plnkr.co/edit/VEMiDcV7x4ixo2QRMqkd
Btw, I just realized my Plunkr was broken due to a new zone.js version (see https://stackoverflow.com/a/45078142/128709). Fixed it by specifying the zone.js version. Sry for the inconvenience.
jasonaden commentedon Jul 26, 2017
@oliverhanappi Thanks for the update to the Plunker. Definitely looks like a valid issue we'll have to take a look at.
imcotton commentedon Sep 21, 2017
also: #13011
atscott commentedon May 28, 2020
Confirmed in v9: https://stackblitz.com/edit/angular-ivy-9pwbpa?file=src%2Fapp%2Fapp.ts
atscott commentedon May 29, 2020
relativeLinkResolution: 'corrected'is the fix for the issue described here. I've opened #37355 to make this option the default.https://stackblitz.com/edit/angular-ivy-v6dwan?file=src/app/app.ts
angular-automatic-lock-bot commentedon Jun 30, 2020
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.