Skip to content

Router: Relative navigation to current state with empty path and different parameters fails  #18059

Closed

Description

@oliverhanappi

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 component TestComponent

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

http://plnkr.co/edit/z4CtoWaDNJMWLAfYKnoP

  1. Open the Plunkr and launch the application in a separate window
  2. Click on "Navigate Relative"
  3. 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

  1. Create the following routing configuration:
export const APP_ROUTES: Routes = [
  {
    path: 'parent',
    children: [
      {
        path: 'child',
        children: [
          {
            path: '',
            pathMatch: 'full',
            component: TestComponent
          }
        ]
      }
    ]
  }
];
  1. In TestComponent, call the method navigateRelative:
@Component(....)
export class TestComponent {
  constructor(private router: Router, private route: ActivatedRoute) {
  }
  
  navigateRelative(): void {
    this.router.navigate(['.', {foo: 123}], {relativeTo: this.route});
  }
}
  1. 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 

Activity

  1. self-assigned this
    on Jul 17, 2017
  2. jasonaden commented on Jul 17, 2017

    @jasonaden
    Contributor

    @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:

    If the path is not empty, it works as well.

  3. oliverhanappi commented on Jul 18, 2017

    @oliverhanappi
    Author

    @jasonaden Yes, exactly, but only when the currently activated component is defined by a route with an empty path.

    If you would change

    { path: '', pathMatch: 'full', component: TestComponent }
    

    to the following: (thus switching to a non empty path)

    { path: 'grandchild', component: TestComponent }
    

    the navigation parent/child/grandchild -> /parent/child/grandchild;foo=123 works as expected using:

    this.router.navigate(['.', {foo: 123}], {relativeTo: this.route});
    

    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.

  4. jasonaden commented on Jul 26, 2017

    @jasonaden
    Contributor

    @oliverhanappi Thanks for the update to the Plunker. Definitely looks like a valid issue we'll have to take a look at.

  5. imcotton commented on Sep 21, 2017

    @imcotton
    Contributor

    also: #13011

  6. added this to the Backlog milestone on Jan 23, 2018
  7. atscott commented on May 29, 2020

    @atscott
    Contributor

    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

  8. angular-automatic-lock-bot commented on Jun 30, 2020

    @angular-automatic-lock-bot

    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.

  9. locked and limited conversation to collaborators on Jun 30, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions