angular / angular/angular

Please let HttpParams.set and .append accept one object-like arg with nested data

未关闭
#20,298 14 条评论 15 个 reaction 已指派 0 人 在 GitHub 查看
area: common/http feature feature: under consideration P4
主要语言
TypeScript
星标
101k
派生
27.5k
平均合并
1 天 19 小时
30 天内合并 PR
288

描述

## I'm submitting a...
[ x ] Feature request

## Current behavior

You can only set or append to HttpParams one by one.

## Expected behavior

With Http.get, .post, etc, you could pass any object with nested object-like data as options.params. First level keys would be mapped to query params, and values JSON encoded if they're object-like. I'd like to achieve the same without adding complexity by doing something like:

> new HttpParams.set( { where: { id: [ 1,2,3 ] } } )

## What is the motivation / use case for changing the behavior?

In migrating to HttpClient from Http, I had to add a helper method:
```
private anyToHttpParams(obj: any) { // requires lodash
return Object.entries(obj)
.reduce(
(params, [key, value]) => params.set(
key, _.isObjectLike(value) ? JSON.stringify(value) : value
),
new HttpParams()
);
}

// this.http.get(url, { params: this.anyToHttpParams({ where: { id: [ 1,2,3 ] } }) } );
```

For me, this was sort of a breaking change as well, as I didn't get a compiler error when I just replaced Http with HttpClient and passed objects with nested data to params like I used to with Http, but in network console my xhr URLs changed to something like `/action?[object object]`.

## Environment

Angular version: 5.0.1

贡献指南

打开贡献指南

调研方向

Start with the HttpParams.set and append APIs and the HttpClient options.params path described in the issue. Done means one object-like argument maps first-level keys to query parameters and JSON-encodes object-like values, including nested data.

由索引模型根据 Issue 内容生成。

评估

技术栈
typescript
领域
api
Issue 类型
功能
难度
4/5
预计耗时
3-5 天
活跃度
停滞
描述清晰度
基本清楚
新手友好度
35/100

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。