node.js - Angular2教程中,这里返回值为什么是heroes?
PHPz
PHPz 2017-04-17 15:25:43
0
2
602

点这里看文档Angular2文档
文件详情在教程最后面

app/hero.service.ts

export class HeroService {
  getHeroes(): Promise<Hero[]> {
    return Promise.resolve(HEROES);
  }
}

如上述代码,getHeroes()返回一个承诺,resolve之前是HEROES

app/app.component.ts
 getHeroes(): void {
    this.heroService.getHeroes().then(heroes => this.heroes = heroes);
  }

据我了解,=>是匿名函数 那么heroes => this.heroes = heroes就大致等价于

function(heroes) {
    return this.heroes = heroes
}

也就是说服务的getHeroes()返回的结果应该是是heroes。
那么问题来了,
明明是 return Promise.resolve(HEROES)
为什么在使用的时候又是heroes?

PHPz
PHPz

学习是最好的投资!

reply all(2)
PHPzhong

app/hero.service.ts

export class HeroService {
getHeroes(): Promise<Hero[]> {

return Promise.resolve(HEROES);

}
} Is this es6 syntax or typescript syntax?

迷茫

Can you understand it this way: the service returns Promise.resolve(HEROES), and the returned data is passed to this.heroes as a parameter of the function

function(heroes) {
    return this.heroes = heroes
}
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template