Services

Let’s take a look at Angular services: how they are structured, and how decorators are used for defining services in Angular.

What are the services?


Services are single TypeScript classes used to access information and share it between components.


A service is simply a TypeScript class, similar to how a component is a TypeScript class.

Create modularity and reusability

The main difference between a component class and a service is that services are used to create more modularity and reusability within the application. They are used as a way to share functionality that may be needed more than once throughout the application. This helps to improve the modularity of the application by dividing it into reusable and standalone services that components can call in. Each service performs a single piece of the app’s functionality. This leads to the app being divided up into these smaller services, rather than having all the logic of the app in one large, monolithic service or component.

Services are there to do only one thing well. This means that the component can load in services to do the one piece of logic the component requires; then, the component can call another service to perform another piece of logic that the component needs. This leads to a component needing to have access to multiple services. Angular loads services into components through Dependency Injection.

Dependency Injection


Dependency Injection (DI) is the method that Angular uses to tell components what services they can consume.


DI is not just an Angular-specific concept; there are many frameworks that use dependency injection, and not just ...