When making an HTTP request with query params in Angular using the HttpClient
you don't need to compose the URL by yourself. Instead, you can pass the params as an object or an HttpParams
instance.
// Instead of passing query params as part of the URL...
function fetchSomeData() {
return this.httpClient.get('/my/endpoint?id=2');
}
// ... you can pass them as object
function fetchSomeData() {
const params: { id: 2 };
return this.httpClient.get('/my/endpoint', { params });
}
// ... or HttpParams
function fetchSomeData() {
const params = new HttpParams().set('id', 2);
return this.httpClient.get('/my/endpoint', { params });
}