在 Angular 中将字符串作为路由参数传递而不进行编码

Passing strings as route parameters in Angular without encoding

我正在尝试使用以下代码传递字符串:

  this.router.navigate(['/systems', params.toString()]);

路由参数然后附加到 URL 用于 API 调用。 params.toString()change_user=2258。但是,当它通过浏览器发送时,它会在 URL 上更改为 change_user%3D2558。当我使用开发人员工具查看发送到 API 的内容时,它已更改为 NaN.
我如何通过路由器传递一个字符串,以便我可以直接将它附加到我的 API 字符串?

编辑:变量 params 的类型为 URLSearchParams()。 这就是我尝试提取参数的方式:

this.route.paramMap.switchMap((params: ParamMap) => 
this.httpService.getSites(+params.get('params')))
.subscribe(sites => this.sites = sites);

添加带有参数的路由定义 here in the Angular 2 routing documentation

{ path: 'hero/:id', component: HeroDetailComponent }

选项 1

然后您将能够导航到

的路线
let id = 2258;
this.router.navigate(['/hero', id]);

并使用 @angular/router 中的 ActivatedRoute 提取参数 here in the documentation

let id = this.route.snapshot.paramMap.get('id');

选项 2

使用 router.navigateByUrl() 方法 (documentation here)

router.navigateByUrl('/hero/2258');

所以这是我修复它的方法:

我所要做的就是在 +params.get() 调用中取出 +。显然,它所做的是尝试将其转换为数字。现在我可以直接传入我的 params.toString() 了。