2016-11-10 1 views
9

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에서 제공됩니다.

모든 기능 라우팅 모듈에 canActivatedata을 설정하지 않아도 될까요? 이 응용 프로그램의 모든 경로에 대해 "기본"canActivate을 구성하고 싶습니다.

+0

당신은 클래스 또는 헬퍼 함수에서 경로 개체를 만들 수 있습니다. 이것이 가장 단순한 솔루션 일뿐입니다. – estus

+0

@estus 그럼 내가 뭔가를해야한다는 뜻이다 :'RouterModule.forChild ([helperService.getRoute ("feature", FeatureComponent)])'? 그 헬퍼 서비스 (또는 클래스 또는 메소드 ..)는 항상 기본 Guard를 생성 된 경로 객체에 추가합니다. – Joel

+2

예. 나는 개인적으로'RouterModule.forChild ([new AuthenticatedRoute ({path : ..., component : ...})])와 같은 것을 쓸 것이다.' – estus

답변

7
const routes: Routes = [ 
    { 
    path: '', 
    canActivate: [AuthGuard], 
    children: [ 
     { path: '', component: HomeComponent }, 
     { path: 'builds', component: BuildsComponent }, 
     { path: 'files', component: FilesComponent }, 
     { path: 'publications', component: PublicationsComponent } 
    ] 
    }, 
    { path: 'login', component: LoginComponent }, 
    { path: '**', redirectTo: '' } 
]; 
+3

'forChild'를 통해 추가 된 자식 경로는 형제로서 forRoot 경로에 추가되므로 계층 구조 안에 있지 않고 가드를 트리거하지 않기 때문에 현재는 안전하지 않습니다. –

0

나는 내 응용 프로그램의 모든 경로 (하위 모듈에서 정의 된 경로 포함)에 대해 Guard를 동적으로 추가하는 솔루션을 작성했습니다.

this Router Docs을 읽는 동안이 해결책을 알아 냈습니다.

const routes: Routes = [ 
 
    { path: '', redirectTo: 'home', pathMatch: 'full' }, 
 
    { path: 'login', component: LoginComponent, data: { skipGuard: true } }, 
 
    { path: '403', component: ForbiddenComponent, data: { skipGuard: true } }, 
 
    { path: '**', component: NotFoundComponent, data: { skipGuard: true } } 
 
]; 
 

 
@NgModule({ 
 
    imports: [RouterModule.forRoot(routes)], 
 
    exports: [RouterModule], 
 
    providers: [] 
 
}) 
 
export class AppRoutingModule { 
 
    
 
    constructor(router: Router) { 
 
    router.config 
 
     .filter(route => !route.data || !route.data.skipGuard) 
 
     .map(route => this.addGuard(route)); 
 
    } 
 
    
 
    private addGuard(route: Route): void { 
 
    route.canActivate = [AuthGuard].concat(route.canActivate); 
 
    route.canActivateChild = [AuthGuard].concat(route.canActivateChild); 
 
    } 
 
}