简体   繁体   English

父组件中的 Angular 2 routerLink 相对于子路由?

[英]Angular 2 routerLink in parent component relative to child route?

I have:我有:

  • Main component主要成分
  • -- First-Child component with parameter -- 带参数的第一个子组件
  • -- Second-Child component -- Second-Child 组件

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.

相关问题 子组件中的Angular2 RouterLink期望在父组件中定义路由 - Angular2 RouterLink in child component is expecting route to be defined in parent component 父组件和其他父子组件之间的Angular 5 routerLink - Angular 5 routerLink between parent component and other parent - child component 角度routerLink相对于可变路由参数 - Angular routerLink relative to a variable route parameter Angular默认子路由未加载父组件 - Angular default child route not loading parent component Angular Routerlink:将父路由字符串与变量连接起来 - Angular Routerlink : Concatenate Parent Route String with Variable 角度2-子路径的父组件与父组件不同 - Angular 2 - Child route have a different parent component to parent component 通过routerLink将父组件数据传递给子组件 - Passing parent component data to child via routerLink Angular-使用routerLink在父组件上获取事件 - Angular - Get Event on Parent Component using routerLink Angular 2在子组件中为[routerLink]模板化的正确方法 - Angular 2 proper way to template a [routerLink] in a child component 子模块中的Angular 6 routerlink和父模块中的router-out(app.component) - Angular 6 routerlink in child module and router-outlet in parent (app.component)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM