add Subscribe in HeroesComponent

This commit is contained in:
Костя 2025-06-19 10:03:28 +03:00
parent 81837bdc60
commit 7f6909208e
2 changed files with 10 additions and 5 deletions

View File

@ -1,15 +1,19 @@
import { Injectable } from '@angular/core';
import { Hero } from './hero';
import { HEROES } from './mock-heroes';
import { Observable, of } from 'rxjs';
@Injectable({
providedIn: 'root'
})
export class HeroService {
getHeroes(): Hero[] {
return HEROES;
}
getHeroes(): Observable<Hero[]> {
const heroes = of(HEROES);
return heroes;
}
constructor() { }
}

View File

@ -21,12 +21,13 @@ export class HeroesComponent { //определяем класс со списк
}
getHeroes(): void {
this.heroes = this.heroService.getHeroes();
this.heroService.getHeroes()
.subscribe(heroes => this.heroes = heroes);
}
ngOnInit(): void {
this.getHeroes();
}
}
constructor(private heroService: HeroService) {}
}