- 提供HTTP服务
- http是angular的HTTP库所提供的一系列服务之一
import { Http, Response, Headers, RequestOptions} from '@angular/http';
- 注册http服务:把HttpModule添加到AppModule的imports列表中
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { HttpModule } from '@angular/http';
import { AppComponent } from './app.component';
@NgModule({
imports: [
BrowserModule,
HttpModule
],
declarations: [
AppComponent
],
bootstrap: [
AppComponent
],
exports: []
})
export class AppModule { }
getHeroes(): Promise<Hero[]> {
return this.http.get(requestUrl)
.toPromise()
.then(response => response.json().data as Hero[])
.catch(this.handleError)
}
//从返回的response中提取数据,通过then的方式提取
.then(response => response.json().data as Hero[])