xxxxxxxxxx
import { Injectable } from '@angular/core';
@Injectable({
providedIn: 'root'
})
export class TestService {
importantValue:number = 42;
constructor() { }
returnImportantValue(){
return this.importantValue;
}
}
xxxxxxxxxx
>>> ng generate service service_name
or
>>> ng generate service /folder-name/service-name
xxxxxxxxxx
For Creating Service you have type command like below
ng generate service service-name
or
ng g s service-name
And if you want to make service into folder
ng generate service /folder-name/service-name
or
ng g s /folder-name/service-name
xxxxxxxxxx
@Injectable({
providedIn: 'root'
})
export class DataService {
private anime: Anime;
constructor() { }
setAnime(anime: Anime) {
this.anime = anime;
}
getAnime() {
return this.anime;
}
}
xxxxxxxxxx
import { <service name>Service } from '../<service path>';
you can inject service in components constructor:
constructor(public anyService: Service){}