如何使用Angular 2 ActivatedRoute更改url中的id?

如何使用Angular 2 ActivatedRoute更改url中的id?,angular,url,typescript,angular-routing,Angular,Url,Typescript,Angular Routing,我想在不更改url的情况下更改url中的参数。 我有这个类型脚本代码: onRowClicked(event: any) { let currentIdPerson = event.data.IdPerson; } 我想把IdPerson发送到URL,我该怎么做?订阅activatedRoute的URL。 解析数组的路径。替换数组中的id。 加入数组并替换url。 “this.location”是位置的一种类型 this.activatedRoute.url.first().subs

我想在不更改url的情况下更改url中的参数。

我有这个类型脚本代码:

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

我想把IdPerson发送到URL,我该怎么做?

订阅activatedRoute的URL。 解析数组的路径。替换数组中的id。 加入数组并替换url。 “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('/'));


        });

Es6支持location.go方法 因此,从@angular/common模块导入构造函数组件中的位置

constructor(private location: Location){}

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

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

这取决于路由配置的外观,哪个组件包含routerLink或
router.navigate()
调用。如果我这样说:
This.router.navigate(['persons',CurrentIderson])
然后它将替换id后面的url的其余部分,我不想这样,我想保留它也许你想要
this.router.navigate(['/persons',currentIderson])
,但正如前面所说的,这取决于你的路由配置和代码的执行位置。我有这样的配置`{path:'persons',component:PersonsComponent,子项:[{path:'',重定向到:'0/identification',pathMatch:'full'},{path:':id/identification',component:IdentificationDataComponent},{path:':id/account',component:AccountDataComponent}]`在父app-routing.module中,从子组件执行
onRowClicked
代码