带可选参数的角路由器

时间:2020-04-15 04:38:31

标签: angular angular-ui-router angular8

是否有一种方法可以完成这种方法而又无需复制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?,那么它也不起作用。

有更好的方法吗?

1 个答案:

答案 0 :(得分: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');