私は、角度2.0セクション5のサービスからHerosチュートリアルをフォローしています。角度2.0のデモチュートリアル - 説明
ドキュメントは仕事をdoesntの関数内this.heroes
constructor(private heroService: HeroService) { }
this.heroService.getHeroes().then(heroes => this.heroes = heroes);
をラップするためにその必要はないと言います。エラーが発生しました
Error: TypeError: Cannot read property 'getHeroes' of undefined(…)
私がonInitを使用すると、それは動作します(セクションの完了)。
constructor(private heroService: HeroService) {
}
ngOnInit() {
this.getHeroes();
}
getHeroes() {
this.heroService.getHeroes().then(heroes => this.heroes = heroes);
}
なぜ結果がngOnInitで実行されているのですか?