[英]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 First-Child 的路径及其参数: http://localhost/mpm/0776452c
Second-Child path should be: http://localhost/mpm/0776452c/settings第二子路径应该是: http://localhost/mpm/0776452c/settings
My problem is that I want to put the link for Second-Child component in Main component.我的问题是我想将 Second-Child 组件的链接放在 Main 组件中。
I tried this way: [routerLink]="'./settings'" but the generated link has round brackets in the end for some reason: http://localhost/mpm/0776452c/(settings)我试过这种方式: [routerLink]="'./settings'" 但由于某种原因,生成的链接最后有圆括号: 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 ?你知道如何用简单的方法解决这个问题吗,因为现在我生成了 Second-Child 组件的完整路径?
Thank you!谢谢!
Edit : The link for Second-Child component is placed in Main component.编辑:Second-Child 组件的链接放置在 Main 组件中。 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这个想法是当我在 First-Child 组件( http://localhost/mpm/0776452c )中时,我想单击 Main 组件中的 [routerLink]="'./settings'" 这样我就可以转到http ://localhost/mpm/0776452c/settings
Edit 2 - my router config编辑 2 - 我的路由器配置
{
path: '',
component: MainComponent,
canActivate: [AuthGuard],
children: [
{
path: 'mpm/:id',
component: FirstChild,
canActivate: [AuthGuard]
},
{
path: 'mpm/:id/settings',
component: SecondChild,
canActivate: [AuthGuard]
}
]
}
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.如果您使用“./settings”作为路由,它将尝试导航到 FirstComponent 的子项,您需要使用“../settings”,但即使这样也行不通。
You'll probably have to rework how you do the way your routes work, and make SecondComponent a child of FirstComponent in the routes.您可能需要重新设计路由的工作方式,并使 SecondComponent 成为路由中 FirstComponent 的子项。
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.如果您尝试导航到 ../settings,父路由将是 /mpm,因此它将解析为 /mpm/settings,而不是 /mpm/:id/settings。 The only other way it could work is if you change your routerLink to something like:它可以工作的唯一其他方法是,如果您将 routerLink 更改为以下内容:
routerLink="../{{id}}/settings"
PS, if you don't want to bind to a value, you can omit the brackets. PS,如果不想绑定一个值,可以省略括号。 That way you don't have to wrap your values in quotes.这样你就不必用引号括起你的值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.