Injectable services
Another key building block of Angular services. Following Angular's architecture, services are supposed to encapsulate business logic and application-wide states. Additionally, services are useful to bridge between different components that aren't necessarily a direct parent and child.
As this is your first services-related task, extract the categories
data to an Angular service.
First, generate the CategoriesService
service, as part of the CoreModule
, by executing the following command:
ng generate service modules/core/services/categories
Afterward, the service is created in a file named categories.service.ts
, containing the following content:
import { Injectable } from '@angular/core'; @Injectable({ providedIn: 'root' }) export class CategoriesService { constructor() { } }
As you can see, services in Angular are simply classes decorated with an @Injectable
decorator.
Angular provides a dependency injection solution, which eases the use of services in your app. Dependency...