Search code examples
angularangular-httpclient

What is different that Httpparams set function call directlry or after cemicolmn


I use httpparams in angular 6. and i have one question.

when i use Httpparams with set, it is very difference that set funciton called after Constructor or called variable name.

funtionTest(): any{
    let params: HttpParams = new HttpParams()
            .set('one','one')
            .set('two','two')
            .set('thr','thr');
    ... some request
}

funtionTest2(): any{
    let params: HttpParams = new HttpParams();
    params.set('one','one');
    params.set('two','two');
    params.set('thr','thr');
    ... some request
}

if i called functionTest(), that one two thr describe of querystring

ex) requestUrl?one=one&two=two&thr=thr;

but, if i called functionTest2(), any information not shown. i think that one ~ thr querystring is inserted body.

what was difference that ?


Solution

  • HttpParams are immutable : it means you don't modify it, but rather return a new object everytime.

    The first function could be translated to

    funtionTest(): any{
        let params: HttpParams = new HttpParams();
        params = params.set('one','one');
        params = params.set('two','two');
        params = params.set('three','three');
        ...
    }
    

    That's why your second function doesn't work : you don't assign the return value to anything.

    To know that, simply check the signature of the set function with a Ctrl + Click !