ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
## Http 在底层使用`XMLHttpRequest`发送http请求。 `Http`作为一个类服务,提供了发送http请求所需的方法。调用`request`方法返回一个`Observable`对象, ### 如何使用 ```typescript import {Http, HTTP_PROVIDERS} from '@angular/http'; import 'rxjs/add/operator/map' @Component({ selector: 'http-app', viewProviders: [HTTP_PROVIDERS], templateUrl: 'people.html' }) class PeopleComponent { constructor(http: Http) { http.get('people.json') // Call map on the response observable to get the parsed people object .map(res => res.json()) // Subscribe to the observable to get the parsed people object and attach it to the // component .subscribe(people => this.people = people); } } ``` ### 类定义 ```typescript class Http { constructor(_backend: ConnectionBackend, _defaultOptions: RequestOptions) request(url: string|Request, options?: RequestOptionsArgs) : Observable<Response> get(url: string, options?: RequestOptionsArgs) : Observable<Response> post(url: string, body: any, options?: RequestOptionsArgs) : Observable<Response> put(url: string, body: any, options?: RequestOptionsArgs) : Observable<Response> delete(url: string, options?: RequestOptionsArgs) : Observable<Response> patch(url: string, body: any, options?: RequestOptionsArgs) : Observable<Response> head(url: string, options?: RequestOptionsArgs) : Observable<Response> options(url: string, options?: RequestOptionsArgs) : Observable<Response> } ``` ```typescript http.get('people.json').subscribe((res:Response) => this.people = res.json()); ``` 默认构造方式用于发送请求,`XMLHttpRequest`被抽象为'Backend'(这里指`XHRBackend`),它可以通过替换`XHRBackend`供应商使用依赖注入方式来模拟,参考如下: ```typescript import {BaseRequestOptions, Http} from '@angular/http'; import {MockBackend} from '@angular/http/testing'; var injector = Injector.resolveAndCreate([ BaseRequestOptions, MockBackend, {provide: Http, useFactory: function(backend, defaultOptions) { return new Http(backend, defaultOptions); }, deps: [MockBackend, BaseRequestOptions]} ]); var http = injector.get(Http); http.get('request-from-mock-backend.json').subscribe((res:Response) => doSomething(res)); ``` ### 属性 - request(url: `string|Request`, options?: `RequestOptionsArgs`) : `Observable<Response>` 执行任何合法的http请求。第一个参数为必填,值为url或者`Request`实例。如果当前传入参数为一个url,那么可以提供一个`RequestOptions`对象类型作为第二个可选参数,在发送请求前它将最终同`BaseRequestOptions`对象合并。 - get(url: `string`, options?: `RequestOptionsArgs`) : `Observable<Request>` 发送http的`get`请求方式 - post(url: `string`, body: any, options?: `RequestOptionsArgs`) : `Observable<Request>` 发送http的`post`请求方式 - put(url: `string`, body: any, options?: `RequestOptionsArgs`) : `Observable<Request>` 发送http的`put`请求方式 - delete(url: `string`, options?: `RequestOptionsArgs`) : `Observable<Request>` 发送http的`delete`请求方式 - patch(url: `string`, body: any, options?: `RequestOptionsArgs`) : `Observable<Request>` 发送http的`patch`请求方式 - head(url: `string`,, options?: `RequestOptionsArgs`) : `Observable<Request>` 发送http的`head`请求方式 - options(url: `string`,, options?: `RequestOptionsArgs`) : `Observable<Request>` 发送http的`options`请求方式