繁体   English   中英

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

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

我有:

  • 主要成分
  • -- 带参数的第一个子组件
  • -- Second-Child 组件

First-Child 的路径及其参数: http://localhost/mpm/0776452c

第二子路径应该是: http://localhost/mpm/0776452c/settings

我的问题是我想将 Second-Child 组件的链接放在 Main 组件中。

我试过这种方式: [routerLink]="'./settings'" 但由于某种原因,生成的链接最后有圆括号: http://localhost/mpm/0776452c/(settings)

你知道如何用简单的方法解决这个问题吗,因为现在我生成了 Second-Child 组件的完整路径?

谢谢!

编辑:Second-Child 组件的链接放置在 Main 组件中。 这个想法是当我在 First-Child 组件( http://localhost/mpm/0776452c )中时,我想单击 Main 组件中的 [routerLink]="'./settings'" 这样我就可以转到http ://localhost/mpm/0776452c/settings

编辑 2 - 我的路由器配置

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

如果您使用“./settings”作为路由,它将尝试导航到 FirstComponent 的子项,您需要使用“../settings”,但即使这样也行不通。

您可能需要重新设计路由的工作方式,并使 SecondComponent 成为路由中 FirstComponent 的子项。

如果您尝试导航到 ../settings,父路由将是 /mpm,因此它将解析为 /mpm/settings,而不是 /mpm/:id/settings。 它可以工作的唯一其他方法是,如果您将 routerLink 更改为以下内容:

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

PS,如果不想绑定一个值,可以省略括号。 这样你就不必用引号括起你的值。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM