0

I have an Angular app where the root directory is a variable route parameter:

/:demo_profile/(etc).

I cannot use relative path because my routerLink is available from different components/routes.

How can I set a routerLink target that is relative to the root of my site, followed by this param? i.e.

/SomeRoute/home

My routes:

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',
  }
];
4

1 回答 1

0

虽然不理想,但解决方案包括获取当前 URL 路径,然后按照以下方式重建路由器链接:

模板中的routerLink:

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

然后参考this.demo_profile使用activatedRoute

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

或者:

activatedRoute.snapshot.url[0].path
于 2019-06-13T07:59:06.130 回答