A service is a class with a specific purpose that provides functionality not directly related to views. Services are used for:
Creating a service:
import { Injectable } from '@angular/core';
@Injectable({
providedIn: 'root'
})
export class DataService {
private data: string[] = [];
getData(): string[] {
return this.data;
}
addData(item: string): void {
this.data.push(item);
}
}