Using behaviour subjects

The HTTP client is using RxJS to return observable streams for all HTTP request methods. The response body will then be emitted through the observable streams, and we can subscribe to the streams to retrieve the results:

this.http.get<Task[]>('/api/tasks')  .subscribe((tasks) => console.log(tasks));

Since we know how to deal with observable streams within our components, we could go ahead and directly return the observable resulting from the HTTP client call.

However, instead, we want to make use of a RxJS class called BehaviorSubject. The problem with directly returning the observable from the HTTP client is that we're always returning a new observable when the tasks are loaded from the server. That would be impractical, ...

Get Mastering Angular Components now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.