我正在嘗試用於Typescript的Anuglar Heroes Tutorial。當試驗服務以下代碼工作:用匿名函數替換箭頭函數導致異常
getHeroes() {
this.heroService.getHeroes().then(heroes => this.heroes = heroes);
}
但是,當我改變的代碼如下它不工作
getHeroes(){
this.heroService.getHeroes().then(function (heroes:Hero[]) {
this.heroes = heroes;
})
}
我收到以下錯誤:
Unhandled Promise rejection: this is null ; Zone: angular ; Task: Promise.then ; Value: TypeError: this is null
this.heroes = heroes;
我已經在課堂裏定義了英雄
heroes: Hero[];
只是爲了瞭解我玩弄的基礎。這清除它 –