简体   繁体   English

angular5 和 http GET 查询参数 - 看起来像专用对象(HttpParams 或 UrlSearchParams)不起作用

[英]angular5 and http GET query parameters - looks like dedicated object (HttpParams or UrlSearchParams) do not work

I have a strange problem I cannot understand.我有一个奇怪的问题,我无法理解。 Here is the code calling REST endpoint:这是调用 REST 端点的代码:

this.http.get<AllApplicationType[]>(environment.SUDS_API_SERVICE_URL + environment.SUDS_ALL_APPLICATIONS_URL, this.setQueryParams(page, size)).toPromise() ...

where setQueryParams function look like this:其中 setQueryParams 函数如下所示:

setQueryParams(page?: number, size?: number): {} {
const startFrom = page * size + 1;
const params = new HttpParams();
params.set('startFrom', startFrom.toString());
params.set('maxRecords', size.toString());
return params;

} }

When the request comes to my backend query params are null, somehow they are not being passed over but why ?当请求到达我的后端查询参数为空时,不知何故它们没有被传递,但为什么? Is this not the right method or what ?这不是正确的方法还是什么?

It just occurred that using HttpParams or URLSearchParams objects do not work at all.碰巧使用 HttpParams 或 URLSearchParams 对象根本不起作用。 All tries with these 2 failed in my case.在我的情况下,所有对这 2 个尝试都失败了。 The only way is to build parameters explicitly like this:唯一的方法是像这样显式构建参数:

setRequestParams(page?: number, size?: number): {} {
const startFrom = page * size + 1;
const params = {'startFrom': startFrom.toString(), 'maxRecords': size.toString()};
const options = {params: params};
return options;

} }

Well I beleive this is a bug in Angular HttpClient.好吧,我相信这是 Angular HttpClient 中的一个错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM