簡體   English   中英

角度routerLink相對於可變路由參數

[英]Angular routerLink relative to a variable route parameter

我有一個Angular應用程序,其中根目錄是一個可變的路由參數:

/:demo_profile/(etc).

我不能使用相對路徑,因為我的routerLink可以從不同的組件/路由獲得。

如何設置相對於我的站點根目錄的routerLink目標,然后是此參數?

/SomeRoute/home

我的路線:

const routes: Routes = [
  {
    path: ':demo_profile',   
    children: [
      {
        path: '',
        redirectTo: 'home',
        pathMatch: 'full'
      },
      {
        path: 'home',    
        component: HomeComponent,
      },
      {
        path: 'cards',    
        component: CardsComponent,
      },
      {
        path: 'card/:id',    
        component: CardComponent,
      },
      {
        path: 'help',    
        component: HelpComponent,
      },
      {
        path: '**',    
        redirectTo: ':demo_profile',
      }
    ]
  },
  {
    path: '\**',    
    redirectTo: '/DGInsureCo/home',
  }
];

雖然不理想,但解決方案包括獲取當前URL路徑,然后按照以下方式重建路由器鏈接:

模板中的routerLink:

<a routerLink="/{{demo_profile}}/home">Home</a>

然后使用activatedRoute引用this.demo_profile

this.route.params.subscribe( params =>
      this.demo_profile = params['demo_profile']
    )

要么:

activatedRoute.snapshot.url[0].path

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM