2017-04-14 93 views
1

我想从url中提取多个参数。假设URL就像Angular 2 - 从url中提取多个变量

www.example.com/parent-component/2/list/child-component/5/list

我想提取的参数25

在父组件的联系是

[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}, 

在下面的部分,我想访问这两个变量movemyid

this.sub = this.activatedRoute.params.subscribe((params : Params) => 
     { 
      this.moveRequestId = params['move']; 

     }); 

在控制台moveRequestId是不确定的 我到底做错了什么?如何访问这些变量?

Router Version :  "@angular/router": "^3.3.1" 
+0

ISSUED_INVENTORY_ROUTES是什么样子的? – DeborahK

回答

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'];