angularjs2进阶教程6-http服务

来源:互联网 发布:知羽电子相册破解版 编辑:程序博客网 时间:2024/06/07 05:55

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

The HttpModule is not a core Angular module. called @angular/http

需要在app.module.ts 上加上

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

还需要

  1. imports:[
  2. HttpModule,
  3. ],
我们伪造一个web服务器
// Imports for loading & configuring the in-memory web api
import { InMemoryWebApiModule } from 'angular-in-memory-web-api';
import { InMemoryDataService }  from './in-memory-data.service';

导入InMemoryWebApiModule并将其加入到模块的imports数组。 InMemoryWebApiModule将Http客户端默认的后端服务 — 这是一个辅助服务,负责与远程服务器对话 — 替换成了内存 Web API服务
InMemoryWebApiModule.forRoot(InMemoryDataService),

利用toPromise操作符把Observable直接转换成Promise对象
import 'rxjs/add/operator/toPromise';

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



0 0
原创粉丝点击