import { Injectable } from '@angular/core'; import { Hero } from './hero'; import { HEROES } from './mock-heroes'; import { Observable, of } from 'rxjs'; import { MessageService } from './message.service'; import { HttpClient } from '@angular/common/http'; @Injectable({ providedIn: 'root' }) export class HeroService { private heroesUrl = 'api/heroes'; // URL to web api constructor( private http: HttpClient, private messageService: MessageService) { } // getHeroes(): Observable { // const heroes = of(HEROES); // this.messageService.add('HeroService: fetched heroes'); // return heroes; // } /** GET heroes from the server */ getHeroes(): Observable { return this.http.get(this.heroesUrl) } getHero(id: number): Observable { // For now, assume that a hero with the specified `id` always exists. // Error handling will be added in the next step of the tutorial. const hero = HEROES.find(h => h.id === id)!; this.messageService.add(`HeroService: fetched hero id=${id}`); return of(hero); } /** Log a HeroService message with the MessageService */ private log(message: string) { this.messageService.add(`HeroService: ${message}`); } }