簡體   English   中英

Angular路由器帶可選參數

[英]Angular router with optional parameter

有沒有辦法在不復制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 hcid 的代碼重復。 我試圖有一個 function 來創建這兩個對象,但 Angular 抱怨我不能在模板中有方法。

如果我將路由用作member-profile/:mcid/:hcid? 它也不起作用。

有什么更好的方法嗎?

// 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');

暫無
暫無

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

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