angular / angular/angular

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

Đang mở
#20,298 14 bình luận 15 reaction 0 người được giao Xem trên GitHub
area: common/http feature feature: under consideration P4
Ngôn ngữ chính
TypeScript
Star
101k
Fork
27.5k
Merge trung bình
1 ngày 19 giờ
Pull request đã merge (30 ngày)
288

Mô tả

## 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

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Hướng nghiên cứu

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.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Đánh giá

Công nghệ
typescript
Lĩnh vực
api
Loại issue
Tính năng
Độ khó
4/5
Thời gian dự kiến
3-5 ngày
Mức độ hoạt động
Đình trệ
Độ rõ ràng
Khá rõ ràng
Mức phù hợp với người mới
35/100

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.