簡體   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