Creating a Service to Retrieve Data

In this section, we will use ng generate to create the PostsService, use environment to store the API URL, and use the PostsService in our components. We will then define our API calls in the PostsService and leverage the HttpClientModule to enable HTTP access.

Generating the Service

We will use the ng generate service command to generate a service that will handle the interaction with our API:

  1. Open your terminal and navigate to the project directory.
  2. Run the following command from inside the project directory:
    ng g s posts/services/posts --module posts/posts
    Generating the Service

Storing Our API URL

We will use the environment of Angular ...

Get Beginning Server-Side Application Development with Angular 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.