我正在尝试用于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[];
只是为了了解我玩弄的基础。这清除它 –