Angular2에서 경로를 구성 할 때 "기본"canActivate
을 설정하는 방법이 있습니까? 모든 경로가 동일한 기본 검사로 덮여 지도록하고 각 경로마다보다 세부적인 검사를 할 수 있습니다.Angular 2 애플리케이션의 모든 경로에 기본 가드 사용
@NgModule({
imports: [
RouterModule.forRoot([
{
path: '',
component: HomeComponent,
canActivate: [AuthenticationGuardService],
data: { roles: [Roles.User] }
}
])
],
exports: [
RouterModule
]
})
export class AppRoutingModule { }
을 그리고 기능 모듈 FeatureModule
을 위해 :
AppModule
이 사용자가 역할을 사용하여 경로에 대한 액세스 권한이있는 경우 내가
AuthenticationGuardService
검사를 할 수
@NgModule({
imports: [
RouterModule.forChild([
{
path: "feature",
component: FeatureComponent,
// I'd like to avoid having to do this:
canActivate: [AuthenticationGuardService],
data: { roles: [Roles.User] }
}
])
],
exports: [
RouterModule
]
})
export class FeatureRoutingModule { }
data
에서 제공됩니다.
모든 기능 라우팅 모듈에 canActivate
과 data
을 설정하지 않아도 될까요? 이 응용 프로그램의 모든 경로에 대해 "기본"canActivate
을 구성하고 싶습니다.
당신은 클래스 또는 헬퍼 함수에서 경로 개체를 만들 수 있습니다. 이것이 가장 단순한 솔루션 일뿐입니다. – estus
@estus 그럼 내가 뭔가를해야한다는 뜻이다 :'RouterModule.forChild ([helperService.getRoute ("feature", FeatureComponent)])'? 그 헬퍼 서비스 (또는 클래스 또는 메소드 ..)는 항상 기본 Guard를 생성 된 경로 객체에 추가합니다. – Joel
예. 나는 개인적으로'RouterModule.forChild ([new AuthenticatedRoute ({path : ..., component : ...})])와 같은 것을 쓸 것이다.' – estus