In the previous snippet we typed the result of fetch call using casting:
We can use the resource generics parameter instead:
The first parameter is used to type the returned value of the Promise
The second parameter can be used to type the request parameters (but we will use it in the next examples, so since there are no parameters we can just type it as void)
resource API also offers a convenient reload method to re-fetch:
Open the Network browser dev tools to see how it works.
Furthermore the isLoading property becomes true again when data are fetched, and returns false value when the operation is completed
We can also set up the endpoint to be called automatically based on a signal value
In the following example we specify that the request is linked to the value of the signal userId.
When the signal is updated, data is automatically re-fetched.
Since the url is composed by (API/[user id]), the endpoint will loads a different user at each API call
src/app.component.ts
The request type
In the previous snippet we typed the request parameter, that is userId, using the second generic parameter:
As you can see now the request parameter is typed as number:
We can also cancel an http calls that is in progress using AbortSignal.
When a new API call is started, the previous one, that is potentially still in progress, will be aborted: