Свободен Роб
Свободен Роб

Reputation: 2810

Angular 2 routerLink in parent component relative to child route?

I have:

The path of the First-Child and it's parameter: http://localhost/mpm/0776452c

Second-Child path should be: http://localhost/mpm/0776452c/settings

My problem is that I want to put the link for Second-Child component in Main component.

I tried this way: [routerLink]="'./settings'" but the generated link has round brackets in the end for some reason: http://localhost/mpm/0776452c/(settings)

Do you have idea how to fix this the easy way, because now I generate full path to Second-Child component ?

Thank you!

Edit: The link for Second-Child component is placed in Main component. The idea is when I'm in First-Child component (http://localhost/mpm/0776452c) I want to click on [routerLink]="'./settings'" which is in Main component so I can go to http://localhost/mpm/0776452c/settings

Edit 2 - my router config

{
  path: '',
  component: MainComponent,
  canActivate: [AuthGuard],
  children: [
    {
      path: 'mpm/:id',
      component: FirstChild,
      canActivate: [AuthGuard]
    },
    {
      path: 'mpm/:id/settings',
      component: SecondChild,
      canActivate: [AuthGuard]
    }
  ]
}

Upvotes: 1

Views: 7755

Answers (1)

smoyer
smoyer

Reputation: 8242

If you use "./settings" as your route, it's going to try to navigate to a child of FirstComponent, you need to use "../settings", but even that won't work.

You'll probably have to rework how you do the way your routes work, and make SecondComponent a child of FirstComponent in the routes.

If you try to navigate to ../settings, the parent route is going to be /mpm, so it'll resolve to /mpm/settings, not /mpm/:id/settings. The only other way it could work is if you change your routerLink to something like:

routerLink="../{{id}}/settings"

P.S, if you don't want to bind to a value, you can omit the brackets. That way you don't have to wrap your values in quotes.

Upvotes: 1

Related Questions