【问题标题】:Angular routerLink relative to a variable route parameter相对于可变路由参数的角度路由器链接
【发布时间】:2019-10-27 17:53:00
【问题描述】:

我有一个 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',
  }
];

【问题讨论】:

    标签: angular router angular-routerlink


    【解决方案1】:

    虽然不理想,但解决方案包括获取当前 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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-05-11
      • 2018-05-17
      • 1970-01-01
      • 2021-07-21
      • 2019-01-22
      • 2019-12-07
      相关资源
      最近更新 更多