Angular 2 - sub-sub-route缺少param

时间:2017-07-24 09:02:39

标签: angular routing

我发现很难让子子路径参数工作

前两个级别工作正常

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 },
            ]
        },
      ]
    }
];

任何人都知道如何解决这个问题?

感谢

2 个答案:

答案 0 :(得分: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(...)

答案 1 :(得分:0)

我把这两个参数都合并了,但正如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)});