Angular 2 - 从url中提取多个变量

时间:2017-04-14 16:18:23

标签: angular angular2-routing

我想从url中提取多个参数。假设网址就像

<option key={value.name + value.description}>

我想提取参数www.example.com/parent-component/2/list/child-component/5/list2

父组件上的链接是

5

在相应的路线文件中,我正在执行此操作

[routerLink]="['/loggedIn','warehouse','move-request',moveRequest.id, 'issued-inventory',issuedInventory.id,'list']"

子路径文件是

{path:':move/issued-inventory',component:IssueInventoryComponent, children : ISSUED_INVENTORY_ROUTES}

在以下组件中,我想要访问变量{path:':myid/list',component:ListIssueInventoryComponent}, move

myid

在控制台上this.sub = this.activatedRoute.params.subscribe((params : Params) => { this.moveRequestId = params['move']; }); 未定义 我究竟做错了什么?如何访问这些变量?

moveRequestId

1 个答案:

答案 0 :(得分:2)

您应首先检索父路由以提取父路由参数。但在此之前,您需要router实例来获取当前路由父ActivatedRoute

constructor(private activatedRoute: ActivatedRoute, private router: Router){

}

this.sub = this.activatedRoute.params.subscribe((params : Params) => {
    //retrieve parent activated route
    const parentRoute = this.router.routerState.parent(activatedRoute);
    this.moveRequestId = params.params['move'];
});

相反,你可以只引用当前激活的路由snapshot属性,它已经引用了父路由。

let move = this.activatedRoute.parent.params['move'];