In yesterday’s code challenge, our application wasn’t displaying any data. There were two reasons for that problem:
HttpClient
was not provided in the application- The change detection strategy
OnPush
wasn’t compatible with the data flow of the application
I’m including links to previous newsletter entries for deeper dives on the different topics involved in that solution. And there are quite a few subtleties to overcome.
Let’s tackle the first issue, which was providing the HTTP client. We can use one of the several utility functions for standalone app config, in our case, provideHttpClient
:
The second problem can be solved in two different ways:
- Remove change detection
onPush
to use thedefault
one instead - Use the
async
pipe or Signals to trigger change detection in thatOnPush
component
Because it’s always better to use the async
pipe, I’m going with that option in our template:
In our component class, I stored the Observable from our service instead of storing the data. No more constructor needed:
You can see that solution in action on Stackblitz here. We fixed our code and added some performance improvements as well by ensuring we can’t get into memory leaks and optimizing change detection for that component.