2
나머지 api에서 데이터를 가져 오는 서비스를 사용하는 각도 2 구성 요소가 있습니다.관찰 가능한 배열에 요소 추가 typescript
import { OnInit, Component } from '@angular/core';
import { Hero } from './hero';
import { HeroService } from './hero.service2';
import { Observable } from 'rxjs';
@Component({
selector: 'my-list',
templateUrl: 'app/hero-list.component.html',
})
export class HeroListComponent implements OnInit {
errorMessage: string;
heroes: Observable<Hero[]>;
mode = 'Observable';
constructor (
private heroService: HeroService
) {}
ngOnInit() { this.getHeroes(); }
getHeroes() {
this.heroes = this.heroService.getHeroes()
}
addHero (name: string) {
if (!name) { return; }
this.heroService.addHero(name)
.subscribe(
hero => this.getHeroes()
);
}
}
addHero를 개선하려면 어떻게해야합니까? 왜냐하면 지금은 매우 비효율적 인 것처럼 보입니다. Observable 영웅에게 this.heroService.addHero()가 반환 한 영웅을 추가하고 싶습니다. 어떻게해야합니까?