-1
我想更改URL中的参数而不更改URL。 如何使用Angular 2 ActivatedRoute更改网址中的ID?
我有这样的打字稿代码:
onRowClicked(event: any) {
let currentIdPerson = event.data.IdPerson;
}
我要发送到的URL IdPerson,我该怎么做呢?
我想更改URL中的参数而不更改URL。 如何使用Angular 2 ActivatedRoute更改网址中的ID?
我有这样的打字稿代码:
onRowClicked(event: any) {
let currentIdPerson = event.data.IdPerson;
}
我要发送到的URL IdPerson,我该怎么做呢?
订阅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('/'));
});
ES6支持位置。去的方法 所以进口位置在构造函数的组件从@角/通用模块
constructor(private location: Location){}
onRowClicked(event :any){
this.location.go(`person\${event.data.personId}\identification`);
}
你的问题是有点不完整,但我想这是我可以给你的最简单的答案
这取决于你的路由配置如何,哪个组件包含routerLink或'router.navigate()'调用。 –
如果我这样说:'this.router.navigate(['persons',currentIdPerson])',那么它会替换后面的url的其余部分,我不想这样,我想保留它 –
也许你想要'this.router.navigate(['/ persons',currentIdPerson])',但如上所述,这取决于你的路由配置以及代码的执行位置。 –