2017-12-05 4 views
0

아래에 나와있는 것처럼 자식 경로를 하드 코드했습니다. 하지만 JSON 개체를 사용하여이를 구축하고 싶습니다. 즉, 아래에 동적으로 추가해야하며 해당 구성 요소를 자동으로 가져와야합니다. JSON 객체도 파일에 저장할 수 있습니다. 요구 사항을 달성 할 수있는 방법이 있습니까?JSON 객체를 사용하여 동적으로 자식 경로를 작성하십시오.

[ 
    { path: 'step1', component: aComponent }, 
    { path: 'step2', component: bComponent }, 
    { path: 'step3', component: cComponent }, 
    { path: 'step4', component: dComponent }, 
    { path: 'step5', component: eComponent }, 
    { path: 'step6', component: fComponent } 
    ] 

최종 결과는 다음과 같아야합니다.

export const abcRoutes: Routes = [ 
     { 
     path: '', 
     component: abcLandingComponent, 
     children: [ 
      { path: 'step1', component: aComponent }, 
      { path: 'step2', component: bComponent }, 
      { path: 'step3', component: cComponent }, 
      { path: 'step4', component: dComponent }, 
      { path: 'step5', component: eComponent }, 
      { path: 'step6', component: fComponent } 
     ] 
     } 
+0

질문을 올바르게 이해했다면, 경로를 정의한 main.ts 파일에이 종류의 모듈을 갖고 싶습니까? 그렇다면 어떻게 경로를 정의 할 것인가? 그것은 뭔가 다른 경로 일 수도 있습니다 : '무언가'? 그 시나리오를 어떻게 다루겠습니까? 또는 경로 속성에 대해 몇 가지 규칙을 설정 했습니까? –

+0

최종 결과 형식으로 내보내기하여 모듈로 가져올 수 있습니까? –

답변

0

이렇게 내보낼 수 있습니까?

import { aComponent } from './aComponent.componet'; 
import { bComponent } from './bComponent.componet';  
etc... 

// or use a barrel file. 

import { aComponent, bComponent, etc.. } from './components'; 

export const childRoutes = [ 
    { path: 'step1', component: aComponent }, 
    { path: 'step2', component: bComponent }, 
    { path: 'step3', component: cComponent }, 
    { path: 'step4', component: dComponent }, 
    { path: 'step5', component: eComponent }, 
    { path: 'step6', component: fComponent } 
]; 

그런 다음 가져 오기 하시겠습니까?

import { childRoutes } from './routes'; 

export const abcRoutes: Routes = [ 
{ 
    path: '', 
    component: abcLandingComponent, 
    children: childRoutes 
}]; 
+0

구성 요소 가져 오기는 어떻습니까? – Hacker

+0

routes.ts에서도이 작업을 수행해야합니다. 두 폴더가 모두 같은 폴더에 있거나 알려진 폴더 구조를 가지고 있다면 가져올 수 있습니다. 배럴 파일을 사용하면 쉽게 사용할 수 있습니다. –