2017-01-31 4 views
1

여기 내 라우팅 모듈.Angular2 라우팅 모듈 구성

@NgModule({ 
    imports: [ 
     RouterModule.forChild([ 
      { 
       path: "", 
       component: AdminComponent, 
       children: [ 
        { 
         path: "users", 
         component: ParentComponent 
         children: [ 
         { 
          path: ":id", 
          component: AnotherComponent 
         } 
         ] 
        } 
       ] 
      }] 
     ) 
    ], 
    exports: [ 
     RouterModule 
    ] 
}) 

export class AdminRoutingModule {} 

이렇게하면, 나는 내 ParentComponent<router-outlet></router-outlet>에 지정해야합니다. 여기서 유일한 문제는 AnotherComponentParentComponent의 일부가 아니라는 것입니다. 두 페이지가 서로 다릅니다. 그래서 더 나은 구조는 같은 수준에서 그들을 가질 수 있습니다. 내가 그 구조로 갔던 이유는 ParentComponent에서만 AnotherComponent에 도착할 수 있었기 때문입니다. 여기에서 가장 현명한 일은 무엇일까요?

+0

문제가되지 않아야합니까? 또한 구성 요소의 코드는 무엇입니까? –

답변

1

이미 귀하의 질문에 언급 그래서 같이 라우터-설정은 다음과 같이한다 : 당신이 당신의 routerLink의이 부분

RouterModule.forChild([{ 
    path: "", 
    component: AdminComponent, 
    children: [ 
     { 
      path: "users", 
      component: ParentComponent 
     }, 
     { 
      path: "users/:id", /* OR WHATEVER .. */ 
      component: AnotherComponent, 
      /* OPTIONAL GUARD */ 
      canActivate: [YourRouteGuard] 
     } 
    ] 
}]) 

배치 방법과 장소! :)

경로를 "보호"하려면 Guards을 사용해야합니다! AnotherComponent을 방지하지 않습니다 parentComponent를 같은 수준에서 AnotherComponent 퍼팅

https://angular.io/docs/ts/latest/guide/router.html#!#milestone-5-route-guards

0

는 parentComponent에에서 도달합니다.