Angular allows you to create services, which are classes that are created for a single purpose. In our project, we have added all of our logic for flash in AppComponent. Suppose we wanted the flash data deep down in the component tree—we would have to pass it down from one component to another. Services make communication between multiple components easier and reduce the logic inside them.
Let's use the Angular Console to generate a service by going to the Generate tab in the side menu, and select Service, and name it as flash and click Generate.
This should create two files: flash.service.ts and flash.service.spec.ts, respectively.
Let's transfer all the logic for flash in the service:
import { Injectable } from '@angular/core';
import { IFlash } from './flash.model';
function getRandomNumber() {
return...