2016-11-10 95 views
2

我有以下網址:angular2子導航欄,路由和路徑參數

業務/信息/聯繫人/:ID

業務/信息/帳號/:ID

業務/細節/地址/:ID

,在我的路由我有以下幾點:

const routes: Routes = [ 
 
    { 
 
     path: 'details', 
 
     component: BusinessDetailsComponent, 
 
     children: [ 
 
      { 
 
       path: 'contacts/:id', 
 
       component: BusinessContactDetailsComponent 
 
      }, 
 
      { 
 
       path: 'account/:id', 
 
       component: BusinessAccountDetailsComponent 
 
      }, 
 
      { 
 
       path: 'address/:id', 
 
       component: BusinessAddressDetailsComponent 
 
      } 
 
     ] 
 
    } 
 
];

這個想法是,BusinessDetailsComponent有一個導航欄在三個頁面之間導航。

我的問題是父母的導航欄需要子路由的ID。我試圖使用下面的代碼從BusinessDetailsComponent中獲取此信息,但這不起作用。它適用於孩子,但不在父母身上。有沒有人有任何想法?

ngOnInit(): void { 
 
     this.route.params.subscribe(params => { 
 
      console.log(params); 
 
     }); 
 
    }

回答

0

作爲一種變通方法,我不得不這樣做下面的技巧:

ngOnInit(): void { 
    // This is a hack that I don't like - need to find a better way! 
    if (this.route.children != null) { 
     this.route.children[0].params.forEach((params: Params) => { 
      this.id = params['id']; 
     }); 
    } 
}