2017-07-24 76 views
1

我覺得真的很難作出一個子子路徑參數工作角2 - PARAM缺失分次路線

這些第一2級做工精細

http://localhost:8080/#/customer/4/ 
http://localhost:8080/#/customer/4/campaign/13 
http://localhost:8080/#/customer/4/campaign/13/segments 

,但一旦我嘗試這一個

http://localhost:8080/#/customer/4/campaign/13/segment/1 

第三個參數/ 1未填充,所以我不能使用它

this.activatedRoute.parent.params.subscribe((params: any) => { 
        let customerId = +params['id']; <<< present 
        let campaignId = +params['cid']; <<< present 
        let segmentId = +params['sid']; <<< NaN !!!! 
        .... 
       }); 

這裏是我的路由設置

const customersRoutes: Routes = [ 
    { path: 'customer/:id', 
     canActivate: [AuthGuardService], 
     children: [ 
     { path: '', component: CustomerComponent }, 
     { path: 'campaign/:cid', component: CustomerCampaignComponent , 
      children:[ 
      { path: 'segments', component: SegmentsComponent }, 
      { path: 'segment/:sid', component: SegmentComponent }, 
      ] 
     }, 
     ] 
    } 
]; 

任何人都知道我怎麼能解決這個問題?

感謝

+0

如果您手動鍵入的URL第三個參數,它的工作與否? –

+0

它正確的路由,但最後的ID永遠不會進入params數組 – phil1234

回答

1

假設你是SegmentComponent

sid不父路徑存在,但存在於路由本身。

this.activatedRoute.params.subscribe((params: any) => { 
    let segmentId = +params['sid'];     .... 
}); 

可以merge的觀測如下:

let obs = this.activatedRoute.params.combineLatest(this.activatedRoute.parent.params) 
obs.subscribe(...) 
+0

另一個角度2路由器奇怪,當我衝浪到父路由,我設法得到cid?我應該有同樣的問題...無論如何,thx,我會合並這兩個流:-) – phil1234

+0

這是一個很好的問題。通常情況下,你必須在'CustomerCampaignComponent'中遇到同樣的問題 –

+0

actualy merge不會合並兩個值,而combineLatest會這樣做......我也嘗試concat,但它只返回一個值 – phil1234

0

我結合這兩種PARAMS喜歡這一點,但作爲Radouane提到了一個能做到這一點,否則

this.activatedRoute.params.combineLatest(this.activatedRoute.parent.params,(val1, val2) => { 
        console.dir(val1); 
        console.dir(val2); 
        return {val1: val1, val2: val2}; 
       }) 
       .subscribe(res => { console.dir(res)});