Assuming that the variable this.http
is indeed of type HttpClient
, we need to consider the type definition for HttpClient.get()
. According to the definition, the options
argument may contain an optional property called params
with the type:
params?: HttpParams | { [param: string]: string | string[]; };
It's important to note that this property cannot be just a single string
, as returned by params.toString()
. It should either be a key-value object containing strings
or an instance of the HttpParams
class.
Since your params
variable aligns with the type
HttpParams</code, you can directly pass it without any conversion.</p>
<p>Instead of:</p>
<pre><code>{ headers: this.headers, params: params.toString() }
You should use:
{ headers: this.headers, params }
However, it seems like in both cases, the params
variable remains empty. Unless there are omitted portions where values are added to it during initialization.
Edit:
After reviewing your complete code, the reason behind the empty state of params
becomes clearer.
In the case of the URLSearchParams
class, the set()
method acts as a void
function, meaning it alters the existing object without returning anything. The original approach involved setting multiple values and then converting the object to a string (segments involving params.set()
were not originally shared).
(method) URLSearchParams.set(name: string, value: string): void
Sets the value associated with a specified search parameter. If other values exist, they get replaced.
Conversely, the behavior of the HttpParams
class differs. Each call to params.set()
creates a new instance rather than updating the current one.
(method) HttpParams.set(param: string, value: string): HttpParams
Substitutes the value for a given parameter.
@param param — Parameter name.
@param value — New value.
@return — Updated body with the fresh value.
The issue here lies in failing to assign the return value of params.set()
back to the main params
variable. To rectify this, replace each occurrence of const params
with let params
to enable overwriting. Furthermore, change all instances of params.set()
to params = params.set()
. For example:
let params: HttpParams = new HttpParams();
params = params.set('page', page.toString());
params = params.set('size', size.toString());
params = params.set('sort', sort);