Angular4: Http -> HttpClient - requestOptions

2020-02-08 11:37发布

问题:

So, I am trying to migrate from 'old' http to the new httpClient

with the http client I am using this format in my service:

return this.http.get(environment.api+ '.feed.json', requestOptions)

how do I use this in httpClient?

tried many thiungs... including

return this.http.get(environment.api+ '.feed.json', {params: requestOptions.params})

but getting a type missmatch :(

回答1:

Try this:

const requestOptions = {
  params: new HttpParams()
};

requestOptions.params.set('foo', 'bar');

this.http.get(environment.api+ '.feed.json', requestOptions );

Here is also the link to the docs describing how to do that with examples for headers and URL Parameters: HttpClient