2017-01-31 157 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>來指定。這裏唯一的問題是AnotherComponent不是ParentComponent的一部分,這是兩個不同的頁面。所以也許一個更好的結構會讓他們處於同一水平。我之所以選擇這種結構是因爲AnotherComponent只能從ParentComponent到達。這裏最明智的做法是什麼?

+0

不應該是一個問題嗎?組件的代碼是什麼? –

回答

1

所以,當你在你的問題已經提到的,您的路由器的配置應該是這樣的:

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

如何以及在哪裏你把你的routerLink的是你的一部分! :)

如果你想「保護」路線,你應該使用Guards

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

0

把AnotherComponent在同一水平爲父級不會阻止AnotherComponent從爲父級僅達到。