Angular 4.3中的Http params [英] Http params in Angular 4.3

查看:224
本文介绍了Angular 4.3中的Http params的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Angular 4.3推出的新http客户端。我检查过文档并使用 HttpParams 类,但似乎没有工作或我做错了什么。以下是我的内容:

I'm trying to use the new http client that came out with Angular 4.3. I've checked the docs and the use the HttpParams class but it seems not to be working or I'm doing something wrong. Here is what I have:

delete(personId: string) {
    const params = new HttpParams();
    params.set('personId', personId);
    return this.http.delete(`${this.baseUrl}`, {
      params
    });
  }

但是在提出请求时,我的网址中没有查询参数
任何帮助将不胜感激

But when making the request there are no query params in my url Any help would be appreciated

推荐答案

params现在是不可变的所以你必须在启动新的时候设置它们HttpParams 所以每个set()都返回一个新实例并应用更改。尝试

The params are now immutable so you have to set them when you initiate the new HttpParams so every set() returns a new instance and applies the changes. Try

const params = new HttpParams()
    .set('personId', personId);

以下是涵盖4.3的标题和网址参数的文档 - https://angular.io/guide/http#headers

Here are the docs that cover headers and url parameters for 4.3 - https://angular.io/guide/http#headers

编辑:我想更新我的回答并声明您在启动HttpParams类时不必设置参数。例如,如果您需要在for循环内或在HttpParams初始化之外设置参数,您可以通过

I wanted to update my answer and state that you don't have to necessarily set the parameters when you initiate the HttpParams class. For example if you need to set parameters within a for loop or outside of the HttpParams initialization you can do so by

const params = new HttpParams()
    .set('personId', personId);

params = params.set('personName', personName);

如文档中所述:


HttpHeaders类是不可变的,因此每个set()都返回一个新实例并应用更改。

The HttpHeaders class is immutable, so every set() returns a new instance and applies the changes.

这篇关于Angular 4.3中的Http params的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆