angularjs2进阶教程6-http服务

前端之家收集整理的这篇文章主要介绍了angularjs2进阶教程6-http服务前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

还是angularjs2入门1-文件结构分析的源码,将app名称tutorial-step6-http

TheHttpModuleisnota core Angular module. called@angular/http

需要在app.module.ts 上加上

import{HttpModule}from'@angular/http';

还需要

  1. imports@H_404_75@:@H_404_75@[
  2. HttpModule@H_404_75@,
  3. @H_404_75@],
我们伪造一个web服务器
// Imports for loading & configuring the in-memory web api
import { InMemoryWebApiModule } from 'angular-in-memory-web-api'; @H_301_105@ import { InMemoryDataService } from './in-memory-data.service';
@H_301_105@
导入InMemoryWebApiModule并将其加入到模块的imports数组。 InMemoryWebApiModule将Http客户端默认的后端服务 — 这是一个辅助服务,负责与远程服务器对话 — 替换成了内存 Web API服务 @H_301_105@
InMemoryWebApiModule.forRoot(InMemoryDataService),@H_301_105@
@H_301_105@
利用toPromise操作符把Observable直接转换成Promise对象 @H_301_105@
import 'rxjs/add/operator/toPromise'; @H_301_105@
@H_301_105@
我们把 getHeroes() 换成用 HTTP
getHeroes(): Promise<Hero[]> {
    return this.http.get(this.heroesUrl)
               .toPromise()
               .then(response => response.json().data as Hero[])
               .catch(this.handleError);
  }

猜你在找的Angularjs相关文章