Skip to content

Router: Cannot read property 'path' of undefined #27674

Closed

Description

@devpreview

🐞 bug report

Affected Package

The issue is caused by package @angular/router

Is this a regression?

No. See issue #22643

Description

If route contain component with empty path:
const routes: Routes = [
  {
    path: '',
    component: IndexComponent
  },
  {
    path: 'parent',
    component: ParentComponent,
    children: [
      {
        path: 'child',
        component: ChildComponent,
        children: [
          {
            path: '', // <-- Error appears here
            component: ThirdComponent
          }
        ]
      }
    ]
  }
];

routerLink directive can create relative url tree.

Image of Reproduction

🔬 Minimal Reproduction

https://angular-router-error.stackblitz.io/parent/child

https://stackblitz.com/edit/angular-router-error

🔥 Exception or Error

ERROR TypeError: Cannot read property 'path' of undefined

🌍 Your Environment

Angular Version:

Angular v7.1.3

Activity

  1. added this to the needsTriage milestone on Dec 14, 2018
  2. jasonaden commented on Dec 18, 2018

    @jasonaden
    Contributor

    @devpreview Thanks for creating a new issue on this. I see there's history on the closed issue, but we typically don't get a chance to go into closed issues so it's much better to just create a new one.

    Based on the history here, I'm adding this to be looked at in one of the upcoming iterations. Due to the holidays in the US, it will likely be just after the New Year, but again, thanks for filing this new issue.

  3. modified the milestones: needsTriage, Backlog on Dec 18, 2018
  4. dariobraun commented on Jan 17, 2019

    @dariobraun
    Contributor

    @jasonaden how would one go about fixing this?

  5. sambaptista commented on Jan 28, 2019

    @sambaptista

    Interesting bug. I share my usecase in case it could help.

    I re-used a component from a project in a new one. Only the routing changed and this bug happened.

    Before it was working with following structure :

    const routes: Routes = [
        { 
            path: 'user',
            component: UsersComponent,
        }
    ]

    New structure don't work :

    const routes: Routes = [
        { 
            path: 'user',
            children :  [
                { 
                    path: '', // <-- error because of this
                    component: UsersComponent,
                }
                ... more stuff here...
            ]
        }
    ]

    My listings use matrix/optional params to store filtering/pagination /users;pageSize=123 by using this.router.navigate(['.', params], {relativeTo: route});

    I will return back to old routing structure for now.

  6. added a commit that references this issue on Jan 28, 2019
  7. giniedp commented on Mar 7, 2019

    @giniedp

    Running into the same error when using relative navigation and changing optional params only

    this.router.navigate([{
      tab: this.activeTab,
    }], {
      relativeTo: this.route,
    })
  8. cyberiny commented on Aug 1, 2019

    @cyberiny

    Can anyone tell if this issue has been resolved, please?

  9. sambaptista commented on Sep 3, 2019

    @sambaptista

    Not yet.

  10. Volodymyr-Mishyn commented on Nov 15, 2019

    @Volodymyr-Mishyn

    Still remains broken in @angular/router 8.0.3 . It's common use case, and there should be a simple way to update optional(matrix) params of current route without clumsy workarounds or redisigning routes hierarchy.

  11. MarkoSulamagi commented on Dec 30, 2019

    @MarkoSulamagi

    Bumping this bug, so the bot wouldn't close it :) Same issue for me on Angular 8.2.*.

  12. paya-cz commented on Jan 19, 2020

    @paya-cz

    I had the same problem. For me, the issue got resolved after using relativeLinkResolution: 'corrected'.

    https://angular.io/api/router/ExtraOptions

    eg.:

    @NgModule({
        imports: [
            RouterModule.forRoot(
                appRoutes,
                {
                    relativeLinkResolution: 'corrected'
                }
            )
        ],
    
        exports: [
            RouterModule
        ]
    })
    export class AppRoutingModule { }
  13. atscott commented on May 29, 2020

    @atscott
    Contributor

    As @paya-cz mentioned, relativeLinkResolution: 'corrected' is the fix for the issue described here. I've opened #37355 to make this option the default.

  14. 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.

  15. 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

No one assigned

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions