From b24b37d4b143823b30ccfb1960d75886a9fe712c Mon Sep 17 00:00:00 2001 From: "DESKTOP-P9VU163\\admin" Date: Thu, 19 Jun 2025 11:50:22 +0300 Subject: [PATCH] final lesson Add services --- src/app/app.component.html | 3 ++- src/app/app.component.ts | 5 ++-- src/app/hero.service.ts | 8 +++--- src/app/heroes/heroes.component.ts | 32 +++++++++++++----------- src/app/message.service.ts | 17 +++++++++++++ src/app/messages/messages.component.css | 19 ++++++++++++++ src/app/messages/messages.component.html | 8 ++++++ src/app/messages/messages.component.ts | 17 +++++++++++++ 8 files changed, 88 insertions(+), 21 deletions(-) create mode 100644 src/app/message.service.ts create mode 100644 src/app/messages/messages.component.css create mode 100644 src/app/messages/messages.component.html create mode 100644 src/app/messages/messages.component.ts diff --git a/src/app/app.component.html b/src/app/app.component.html index a6988be..0fc7503 100644 --- a/src/app/app.component.html +++ b/src/app/app.component.html @@ -1,2 +1,3 @@

{{title}}

- \ No newline at end of file + + \ No newline at end of file diff --git a/src/app/app.component.ts b/src/app/app.component.ts index 63c0da1..98e7175 100644 --- a/src/app/app.component.ts +++ b/src/app/app.component.ts @@ -1,13 +1,14 @@ import { Component } from '@angular/core'; -import { RouterOutlet } from '@angular/router'; import { HeroesComponent } from "./heroes/heroes.component"; +import { MessagesComponent } from "./messages/messages.component"; @Component({ selector: 'app-root', templateUrl: './app.component.html', styleUrls: ['./app.component.css'], - imports: [HeroesComponent] + imports: [HeroesComponent, MessagesComponent] }) + export class App { title = 'Tour of Heroes'; } diff --git a/src/app/hero.service.ts b/src/app/hero.service.ts index 588db16..936e1c1 100644 --- a/src/app/hero.service.ts +++ b/src/app/hero.service.ts @@ -2,6 +2,7 @@ import { Injectable } from '@angular/core'; import { Hero } from './hero'; import { HEROES } from './mock-heroes'; import { Observable, of } from 'rxjs'; +import { MessageService } from './message.service'; @Injectable({ providedIn: 'root' @@ -11,9 +12,10 @@ export class HeroService { getHeroes(): Observable { const heroes = of(HEROES); + this.messageService.add('HeroService: fetched heroes'); return heroes; - } - - constructor() { } +} + + constructor(private messageService: MessageService) { } } diff --git a/src/app/heroes/heroes.component.ts b/src/app/heroes/heroes.component.ts index 9743921..2896eed 100644 --- a/src/app/heroes/heroes.component.ts +++ b/src/app/heroes/heroes.component.ts @@ -1,9 +1,10 @@ -import { Component } from '@angular/core'; +import { Component, OnInit } from '@angular/core'; import { Hero } from '../hero'; import { CommonModule } from '@angular/common'; import { FormsModule } from '@angular/forms'; import { HeroService } from '../hero.service'; //новая библиотека добавлена вместо HEROES import { HeroDetailComponent } from '../hero-detail/hero-detail.component'; +import { MessageService } from '../message.service'; @Component({ selector: 'app-heroes', @@ -12,24 +13,25 @@ import { HeroDetailComponent } from '../hero-detail/hero-detail.component'; imports: [CommonModule, FormsModule, HeroDetailComponent] }) -export class HeroesComponent { //определяем класс со списком героев - heroes: Hero[] = []; +export class HeroesComponent implements OnInit { + selectedHero?: Hero; + heroes: Hero[] = []; + + constructor(private heroService: HeroService, private messageService: MessageService) { } + + ngOnInit(): void { + this.getHeroes(); + } + onSelect(hero: Hero): void { - this.selectedHero = hero; + this.selectedHero = hero; + this.messageService.add(`HeroesComponent: Selected hero Name=${hero.name}`); } getHeroes(): void { - this.heroService.getHeroes() - .subscribe(heroes => this.heroes = heroes); + this.heroService.getHeroes() + .subscribe(heroes => this.heroes = heroes); } - - ngOnInit(): void { - this.getHeroes(); - } - constructor(private heroService: HeroService) {} - -} - - +} \ No newline at end of file diff --git a/src/app/message.service.ts b/src/app/message.service.ts new file mode 100644 index 0000000..eeb09b3 --- /dev/null +++ b/src/app/message.service.ts @@ -0,0 +1,17 @@ +import { Injectable } from '@angular/core'; +import { CommonModule } from '@angular/common'; + +@Injectable({ + providedIn: 'root', +}) +export class MessageService { + messages: string[] = []; + + add(message: string) { + this.messages.push(message); + } + + clear() { + this.messages = []; + } +} \ No newline at end of file diff --git a/src/app/messages/messages.component.css b/src/app/messages/messages.component.css new file mode 100644 index 0000000..80c3b2d --- /dev/null +++ b/src/app/messages/messages.component.css @@ -0,0 +1,19 @@ +/* MessagesComponent's private CSS styles */ +h2 { + color: #A80000; + font-family: Arial, Helvetica, sans-serif; + font-weight: lighter; +} + +.clear { + color: #333; + background-color: #eee; + margin-bottom: 12px; + padding: 1rem; + border-radius: 4px; + font-size: 1rem; +} +.clear:hover { + color: white; + background-color: #42545C; +} \ No newline at end of file diff --git a/src/app/messages/messages.component.html b/src/app/messages/messages.component.html new file mode 100644 index 0000000..a93e05f --- /dev/null +++ b/src/app/messages/messages.component.html @@ -0,0 +1,8 @@ +
+ +

Messages

+ +
{{message}}
+ +
diff --git a/src/app/messages/messages.component.ts b/src/app/messages/messages.component.ts new file mode 100644 index 0000000..2fcab88 --- /dev/null +++ b/src/app/messages/messages.component.ts @@ -0,0 +1,17 @@ +import { Component } from '@angular/core'; +import { MessageService } from '../message.service'; +import { CommonModule } from '@angular/common'; + +@Component({ + selector: 'app-messages', + imports: [CommonModule ], + templateUrl: './messages.component.html', + styleUrl: './messages.component.css' +}) + +export class MessagesComponent { + + constructor(public messageService: MessageService) {} + +} +