search

Home  >  Q&A  >  body text

node.js - Angular2教程中,这里返回值为什么是heroes?

点这里看文档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?

PHPzPHPz2864 days ago614

reply all(2)I'll reply

  • PHPz

    PHPz2017-04-17 15:27:43

    app/hero.service.ts

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

    return Promise.resolve(HEROES);

    }
    } Is this es6 syntax or typescript syntax?

    reply
    0
  • 迷茫

    迷茫2017-04-17 15:27:43

    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
    }
    

    reply
    0
  • Cancelreply