add Update HeroesComponent

This commit is contained in:
Костя 2025-06-19 09:31:36 +03:00
parent fbd8f6bae9
commit 81837bdc60
2 changed files with 18 additions and 3 deletions

View File

@ -1,9 +1,15 @@
import { Injectable } from '@angular/core'; import { Injectable } from '@angular/core';
import { Hero } from './hero';
import { HEROES } from './mock-heroes';
@Injectable({ @Injectable({
providedIn: 'root' providedIn: 'root'
}) })
export class HeroService { export class HeroService {
getHeroes(): Hero[] {
return HEROES;
}
constructor() { } constructor() { }
} }

View File

@ -2,7 +2,7 @@ import { Component } from '@angular/core';
import { Hero } from '../hero'; import { Hero } from '../hero';
import { CommonModule } from '@angular/common'; import { CommonModule } from '@angular/common';
import { FormsModule } from '@angular/forms'; import { FormsModule } from '@angular/forms';
import {HEROES} from '../mock-heroes'; import { HeroService } from '../hero.service'; //новая библиотека добавлена вместо HEROES
import { HeroDetailComponent } from '../hero-detail/hero-detail.component'; import { HeroDetailComponent } from '../hero-detail/hero-detail.component';
@Component({ @Component({
@ -13,13 +13,22 @@ import { HeroDetailComponent } from '../hero-detail/hero-detail.component';
}) })
export class HeroesComponent { //определяем класс со списком героев export class HeroesComponent { //определяем класс со списком героев
heroes = HEROES; heroes: Hero[] = [];
selectedHero?: Hero; selectedHero?: Hero;
onSelect(hero: Hero): void { onSelect(hero: Hero): void {
this.selectedHero = hero; this.selectedHero = hero;
} }
getHeroes(): void {
this.heroes = this.heroService.getHeroes();
}
ngOnInit(): void {
this.getHeroes();
}
constructor(private heroService: HeroService) {}
} }