Angular 4 HttpClient Query Parameters

后端 未结 8 971
耶瑟儿~
耶瑟儿~ 2020-11-29 16:00

I have been looking for a way to pass query parameters into an API call with the new HttpClientModule\'s HttpClient and have yet to find a solution

8条回答
  •  迷失自我
    2020-11-29 16:37

    You can (in version 5+) use the fromObject and fromString constructor parameters when creating HttpParamaters to make things a bit easier

        const params = new HttpParams({
          fromObject: {
            param1: 'value1',
            param2: 'value2',
          }
        });
    
        // http://localhost:3000/test?param1=value1¶m2=value2
    

    or:

        const params = new HttpParams({
          fromString: `param1=${var1}¶m2=${var2}`
        });
    
        //http://localhost:3000/test?paramvalue1=1¶m2=value2
    

提交回复
热议问题