a) Create the HeroService + Provide the HeroService + Update HeroesComponent
--- /dev/null
+import { TestBed } from '@angular/core/testing';
+
+import { HeroService } from './hero.service';
+
+describe('HeroService', () => {
+ beforeEach(() => TestBed.configureTestingModule({}));
+
+ it('should be created', () => {
+ const service: HeroService = TestBed.get(HeroService);
+ expect(service).toBeTruthy();
+ });
+});
--- /dev/null
+import { Injectable } from '@angular/core';
+import { Hero } from './hero';
+import { HEROES } from './mock-heroes';
+
+
+@Injectable({
+ providedIn: 'root'
+})
+export class HeroService {
+
+ constructor() { }
+
+ getHeroes() : Hero[] {
+ return HEROES;
+ }
+}
import { Component, OnInit } from '@angular/core';
import { Hero } from '../hero';
import { Component, OnInit } from '@angular/core';
import { Hero } from '../hero';
-import { HEROES } from '../mock-heroes';
+import { HeroService } from '../hero.service';
@Component({
selector: 'app-heroes',
@Component({
selector: 'app-heroes',
})
export class HeroesComponent implements OnInit {
})
export class HeroesComponent implements OnInit {
this.selectedHero = hero;
}
this.selectedHero = hero;
}
+ constructor(private heroService : HeroService) { }
+ getHeroes() : void {
+ this.heroes = this.heroService.getHeroes();
+ }