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 { HEROES } from '../mock-heroes';
+import { HeroService } from '../hero.service';
@Component({
selector: 'app-heroes',
})
export class HeroesComponent implements OnInit {
- heroes = HEROES;
+ heroes: Hero[];
selectedHero : Hero;
this.selectedHero = hero;
}
- constructor() { }
+ constructor(private heroService : HeroService) { }
ngOnInit() {
+ this.getHeroes();
}
+ getHeroes() : void {
+ this.heroes = this.heroService.getHeroes();
+ }
}