在Angular 2中处理带有多个问号的查询参数

问题描述 投票:2回答:1

所以基本上我用查询参数得不到正确的结果。例如,我们有一个如下所示的查询参数:

?encryptedId=zxcvbnm?:112233

返回的结果只是zxcvbnm,第二个问号和所有后续字符都被省略了。

我已经尝试过了:

this.businessId = this.router.parseUrl(this.router.url).queryParams["bid"];

还有这个:

this.activatedRoute.queryParams.subscribe((queryParams: Params) => {
      this.businessId = queryParams['id'];
    }); 

但两个代码都返回相同的结果。我想抓住一切。请帮忙。谢谢!

angular query-parameters
1个回答
2
投票

您需要在使用encodeURIComponent传入URL之前对其进行编码

可以在浏览器控制台上尝试encodeURIComponent('zxcvbnm?:112233'),你会看到结果。

所以?encryptedId=zxcvbnm?:112233将成为?encryptedId=zxcvbnm%3F%3A112233

然后,从组件中,您可以检索它,如下所示。我测试了它。

export class QueryParamsComponent implements OnInit {
    constructor(
        private router: Router,
        private route: ActivatedRoute) {
    }

    ngOnInit() {
        let queryParams = this.route.snapshot.queryParams;
        const encryptedId = queryParams['encryptedId'];
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.