Http params in Angular 4.3

前端 未结 2 872
星月不相逢
星月不相逢 2020-12-29 11:07

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

2条回答
  •  长情又很酷
    2020-12-29 11:48

    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);
    

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


    Edit: 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);
    

    As stated by the docs:

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

提交回复
热议问题