Return to site

Créer un service

Designed to create reusable logic that can be injected into multiple components

· angular

Services

Introduction to services

Services are a type of component in Angular that are specifically designed to create reusable logic that can be injected into multiple components. This makes them ideally suited for the task of compartmentalizing API interactions, and so that is their most common use case.

 

When you use a service to hold your API interactions, you can easily add additional features to your API methods such as caching, and allow your components to subscribe to a single instance of an API call, preventing unnecessary trips to the server.

 

A common usage scenario for a service in Angular is to fetch data from a data source. This could be a database running on a server somewhere, or it could be any other form of data that you Angular application needs. In the lab for this topic, you will create a service to retrieve simple data from a web site, in JSON format.

Creating a service

Using a service

Tutorial Lab Creating Services

To Create a Service

To Import an Interface into Your Service

To Create a Service Method