2016-10-29 4 views
3

두 형제 하위 경로 사이에 같은 확인자를 공유하는 데 더 효과적인 방법이 있는지 알아 내려고하고 있습니다. 아래는 경로가 리졸버와 어떻게 관련되어 있는지에 대한 예입니다.각도 2 : 동일한 해결자를 공유하는 형제 자식 경로

import { CommonModule } from '@angular/common'; 
import { RouterModule } from '@angular/router'; 
import { NgModule } from '@angular/core'; 
export const routes = [{ 
     path: '', 
     component: parentComponent, 
     canActivate: [AuthGuard], 
     resolve: { 
      someData: someDataResolver 
     }, 
     children: [ 
      { path: '', redirectTo: '0', pathMatch: 'full' }, 
      { path: '0', 
       component: someComponent1, 
       resolve: { 
        someData1: someData1Resolver, 
        someData2: someData2Resolver, 
       } 
      }, 
      { path: '2', 
       component: someComponent2, 
       resolve: { 
        someData2: someData2Resolver 
       } 
      } 
      ... a bunch more children routes/components with resolvers 

     ] 
    }] 

지금 당장은 아이들의 경로를 결정하는 과정을 반복하고 있습니다. 공유 형제 하위 분석가의 데이터를 더 잘 공유 할 수있는 방법이 있는지 알고 있습니까? 중복 된 해결 자에서 공유 서비스로 데이터를 설정하는 방법에 대해 생각했습니다. 그러면 다른 자식 형제 경로가 서비스의 데이터에 액세스합니다 (해결 자에서 다른 API 호출을 만드는 대신). 다른 최적의 솔루션이 있습니까?

답변

5

당신은 componentless 부모 경로와 추가 레벨을 추가 할 수 있으며이 수준에 해결을 이동 :

import { CommonModule } from '@angular/common'; 
import { RouterModule } from '@angular/router'; 
import { NgModule } from '@angular/core'; 
export const routes = [{ 
     path: '', 
     component: parentComponent, 
     canActivate: [AuthGuard], 
     resolve: { 
      someData: someDataResolver 
     }, 
     children: [ 
      { path: '', 
       resolve: { 
        someData2: someData2Resolver 
       }, 

      children: [ 
       { path: '', redirectTo: '0', pathMatch: 'full' }, 
       { path: '0', 
        component: someComponent1, 
        resolve: { 
         someData1: someData1Resolver, 
        } 
       }, 
       { path: '2', 
        component: someComponent2, 
       } 
       ... a bunch more children routes/components with resolvers 
      ] 
     ] 
    }] 
+0

어떻게 할 아이 요소 액세스 상위 구성 요소에서 해결 된 데이터? – Gambo

+3

자식 경로가'route : ActivatedRoute'를 삽입하면'this.route.parent.data' 또는'this.route.parent.snapshot.data'를 사용하여 자식 경로에 접근 할 수 있어야합니다. –

+0

감사합니다! 예를 들어 데이터를 다시로드 하시겠습니까? 엔티티를 편집 한 사용자가 상세 뷰로 다시 이동하고 있습니까? 경로 : 'ID', 구성 요소 : CustomerDetailComponent, 해결 : {고객 : ...}, 어린이 : [ {경로 : '편집', 구성 요소 : CustomerEditComponent ....} ] 데이터 사용자가 상위 경로에 머물러 있기 때문에 다시로드되지 않습니다. – Gambo