带有可选参数的角路由器

问题描述 投票:0回答:1

是否有一种方法可以完成这种方法而又不复制children属性的代码?

const routes: Routes = [
  {
    path: '', component: SmartSearchComponent, canActivate: [AuthGuard],
    canActivateChild: [AuthGuard],
    children: [
      {path: '', redirectTo: 'home', pathMatch: 'full'},
      {path: 'home', component: HomeComponent},
      {
        path: 'member-profile/:mcid',
        component: MemberProfileComponent,
        children: [
          {path: '', redirectTo: 'member-info', pathMatch: 'full'},
          {path: 'member-info', pathMatch: 'full', component: MemberInfoComponent},
          {path: 'id-cards', component: IdCardsComponent},
          {path: 'register-family-members', component: RegisteredFamilyMembersComponent},
          {path: 'associate-caregivers', component: AssociateCaregiversComponent},
          {path: 'member-preferences', component: MemberPreferencesComponent},
          {path: 'two-fa-info', component: TwofaInfoComponent},
          {path: 'coverage/:hcid', component: CoverageComponent},
        ]
      },
      {
        path: 'member-profile/:mcid/:hcid',
        component: MemberProfileComponent,
        children: [
          {path: '', redirectTo: 'member-info', pathMatch: 'full'},
          {path: 'member-info', pathMatch: 'full', component: MemberInfoComponent},
          {path: 'id-cards', component: IdCardsComponent},
          {path: 'register-family-members', component: RegisteredFamilyMembersComponent},
          {path: 'associate-caregivers', component: AssociateCaregiversComponent},
          {path: 'member-preferences', component: MemberPreferencesComponent},
          {path: 'two-fa-info', component: TwofaInfoComponent},
          {path: 'coverage/:hcid', component: CoverageComponent},
        ]
      }
    ]
  }
];

我对member-profile/:mcidmember-profile/:mcid/:hcid的路线有重复的代码,我不太喜欢。我试图创建一个可以创建这两个对象的函数,但是Angular抱怨我无法在模板中使用方法。

如果我将路线用作member-profile/:mcid/:hcid?,则也不起作用。

任何更好的方法?

angular angular-ui-router angular8
1个回答
0
投票
// Declaration 
const appRoutes: Routes = [{ path: employee, component: abcComp }]
// You dont need to set Query parameter in routing
// Implementation in Ts
this.route.navigate(['employee'], { queryParams: { name: 'a' } })
//queryParamsHandling='Merge' or 'retain' these options is also used to retain the parameters or to merge them 
// Catching in Ts 
constructor(private route : ActivateRoute){
}
let name = this.route.snapshot.queryParamMap.get('name');
© www.soinside.com 2019 - 2024. All rights reserved.