繁体   English   中英

Angular 2 RxJs Promise不适用于C#Controller的http.get调用,而Angular 2 In Memory Web API确实可以工作

[英]Angular 2 RxJs Promise not working for http.get call to C# Controller whereas Angular 2 In Memory Web API does work

我正在使用ASP.NET Core和Angular 2(〜2.2.0版)和Rxjs(^ 5.0.0-rc.5版)。 angular.io文档中的代码示例可与Angular 2的In Memory Web API一起使用,但是当我对C#控制器进行http.get调用时,它将失败。 数据正在返回。 以下代码行不起作用:

getHeroes(): Promise<Hero[]> {
return this.http.get(this.heroesUrl)
           .toPromise()
           .then(response => response.json().data as Hero[])
           .catch(this.handleError);
  }

我更改了代码,以下几行代码可以正常工作:

getHeroes(): Hero[] {
    this.http.get(this.heroesUrl).map(response => response.json()).subscribe(heroes => {
        this.heroes = heroes;
    })
    return this.heroes;
}

有人可以帮忙吗? 我真的需要使第一行代码起作用,因为第二行代码无法按计划工作,它们只是演示了数据正在返回并正在显示。

你可以这样使用诺言

return this.http.get(this.heroesUrl)
.map(response => response.json() as Ge)
       .toPromise()
       .then(response => response.json().data as Hero[])
       .catch(this.handleError);

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM