Create an Angular Service to Retrieve Data from an API
In this lesson we will create the
ProductsService that is responsible for the interaction with our API.
We will inject the
HttpClient that we get from
@angular/common/http. To make this working we need to import
HttpClientModule in our
We define a
getProduct method so we can get a list of products and an single product which we will use in respectively the
In both components we will use the
ngOnInit lifecycle hook to invoke the methods on the service, and retrieve the data by subscribing to it.