2016-11-10 17 views
9

Angular2'deki yolları yapılandırırken bir "temel" canActivate ayarlamanın bir yolu var mı? Böylece tüm güzergahlar aynı temel kontrol tarafından karşılanır ve daha sonra her rota daha ayrıntılı bir kontrole sahip olabilir.Angular 2 uygulamasındaki tüm yollar için bir temel koruma kullanma

@NgModule({ 
    imports: [ 
     RouterModule.forRoot([ 
      { 
       path: '', 
       component: HomeComponent, 
       canActivate: [AuthenticationGuardService], 
       data: { roles: [Roles.User] } 
      } 
     ]) 
    ], 
    exports: [ 
     RouterModule 
    ] 
}) 
export class AppRoutingModule { } 

Ve özellik modülü FeatureModule için:

Böyle yönlendirme ile AppModule sahip bir kullanıcı rollerini kullanarak bir rota erişimi varsa ben AuthenticationGuardService çek izin
@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'da sağlanmıştır.

Tüm özellik yönlendirme modüllerinde canActivate ve data ayarını yapmamak için bir şey yapabilir miyim? Bu uygulamadaki tüm yollar için bir "taban" canActivate yapılandırmak istiyorum.

+0

Bir sınıf veya yardımcı işlevinden rota nesnesi oluşturabilir. Bu en basit (ve muhtemelen tek mevcut) çözümdür. – estus

+0

@estus Yani demek istediğim gibi bir şey yapmalıyım: RouterModule.forChild ([helperService.getRoute ("özellik", FeatureComponent)]) '? Ve sonra bu yardımcı hizmet (veya sınıf veya yöntem ..) her zaman temel Guard oluşturulan rota nesnesine ekler? – Joel

+2

Evet. Ben şahsen RouterModule.forChild ([yeni AuthenticatedRoute ({yol: ..., bileşen: ...})]) – estus

cevap

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

Çalışırken, bu şu anda güvenli değildir, çünkü 'forChild 'ile eklenen çocuk rotaları hiyerarşi içinde değil, for the roroot'lar için' forRoot 'rotalarına kardeş olarak eklenir ve muhafazayı tetiklemez. –

0

Uygulamamın her yolunda (çocuk modülleri tarafından tanımlananlar dahil) Guard'ı dinamik olarak eklemek için bir çözüm yazdım.

Bu çözümü this Router Docs okurken çözdüm.

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); 
 
    } 
 
}