## 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`请求方式
- 说明
- angular 1.x
- ngModelController
- ngOptions
- ngModelOptions
- lifecycle
- directive
- angular 2
- @angular/forms
- 类
- AbstractControl
- AbstractControlDirective
- AbstractFormGroupDirective
- FormControl
- FormArray
- FormBuilder
- FormGroup
- NgControl
- 接口
- controlValueAccessor
- 指令
- DefaultValueAccessor
- Angular 2 生命周期
- OnInit
- DoCheck
- @angular/router
- 配置
- Routes
- 指令
- RouterOutlet
- RouterLink
- 接口
- ActivatedRoute
- UrlTree
- NavigationExtras
- ActivatedRouteSnapshot
- RouterStateSnapshot
- 类
- UrlSegment
- UrlSegmentGroup
- UrlSerializer
- DefaultUrlSerializer
- Router
- bug记得
- @angular/http
- 类
- Http
- Body
- Response
- ResponseOptions
- Header
- Request
- RequestOptions
- URLSearchParams
- @angular/core
- decorator
- Component-decorator
- animation
- DI
- linker
- TemplateRef
- ElementRef
- EmbeddedViewRef
- ViewRef
- ViewContainerRef
- Query
- ComponentFactory
- ComponentRef
- Renderer
- change_detection
- KeyValueDiffers
- IterableDiffers
- ChangeDetectorRef
- ChangeDetectionStrategy
- Zone
- ngZone
- @angular/common
- 指令
- NgTemplateOutlet
- QueryList
- bootstrap4
- card
- form
- 重点关注博客
- 学习过的文章
- 笔记
- Angular 2 双向绑定
- 将字符串解析成DOM
- rx相关
- operators
- combineLatest
- combineAll
- concat(All, Map, *MapTo)
- 背压(backpressure)
- js事件keycode对应表
- 装饰器
- 有用的代码摘录
- 日期操作
- 数量操作
- 字符操作
- rxjs问题
- 小示例
- h5面试准备
- react
- 开发遇到的问题