2017-04-21 60 views
-1

我想更改URL中的参数而不更改URL。 enter image description here如何使用Angular 2 ActivatedRoute更改网址中的ID?

我有这样的打字稿代码:

onRowClicked(event: any) { 
    let currentIdPerson = event.data.IdPerson; 
} 

我要发送到的URL IdPerson,我该怎么做呢?

+0

这取决于你的路由配置如何,哪个组件包含routerLink或'router.navigate()'调用。 –

+0

如果我这样说:'this.router.navigate(['persons',currentIdPerson])',那么它会替换后面的url的其余部分,我不想这样,我想保留它 –

+0

也许你想要'this.router.navigate(['/ persons',currentIdPerson])',但如上所述,这取决于你的路由配置以及代码的执行位置。 –

回答

0

订阅activatedRoute的网址。 解析数组的路径。替换数组中的id。 加入数组并替换网址。 “this.location”是一种位置的

this.activatedRoute.url.first().subscribe(url => { 
      let path = this.location.path().split('/'); 
      let index = path.indexOf(url[0].path); 
      path.splice(index, count, currenIdPerson); 


      this.location.replaceState(path.join('/')); 


     }); 
1

ES6支持位置。去的方法 所以进口位置在构造函数的组件从@角/通用模块

constructor(private location: Location){} 

onRowClicked(event :any){ 
this.location.go(`person\${event.data.personId}\identification`); 
} 

你的问题是有点不完整,但我想这是我可以给你的最简单的答案