class RequestOptions – 创建request的参数
class BaseRequestOptions – 具有一些默认值的经典参数
RequestOptionsimport {RequestOptions, Request, RequestMethods} from 'angular2/http';
var options = new RequestOptions({
method: RequestMethods.Post,
url: 'https://google1.com'
});
var req = new Request(options.merge({
url: 'https://google.com'
}));
console.log('req.method:', RequestMethods[req.method]); // Post
console.log('options.url:', options.url); // null
console.log('req.url:', req.url); // https://google.com
- app.ts:10 req.method: Post
- 2016-01-29 11:42:10.601 app.ts:11 options.url: https://google1.com
- 2016-01-29 11:42:10.602 app.ts:12 req.url: https://google.com
注意merge的说明。不覆盖的
constructor({method, headers, body, url, search}: RequestOptionsArgs = {}) { |
注意着3个的类型
headers: Headers; |
BaseRequestOptions
Default values:
- method: RequestMethod.Get
- headers: empty Headers object
覆盖默认的BaseRequestOptions
import {provide} from 'angular2/core'; |
Request
看下构造
constructor(requestOptions: RequestArgs) { |
发出自己的request
import {Injectable, Injector} from 'angular2/core'; |
Options 里的参数
class Headers
就是设置header里面的参数
import {Headers} from 'angular2/http';
var firstHeaders = new Headers();
firstHeaders.append('Content-Type', 'image/jpeg');
console.log(firstHeaders.get('Content-Type')) //'image/jpeg'
// Create headers from Plain Old JavaScript Object
var secondHeaders = new Headers({
'X-My-Custom-Header': 'Angular'
});
console.log(secondHeaders.get('X-My-Custom-Header')); //'Angular'
var thirdHeaders = new Headers(secondHeaders);
console.log(thirdHeaders.get('X-My-Custom-Header')); //'Angular'有个静态方法,估计要和response 配合用
fromResponseHeaderString(headersString: string) : Headers
Returns a new Headers instance from the given DOMString of Response Headers
这个方法试验不了,提示 not a function。 我给angular 提了issue
https://github.com/angular/angular/issues/6767
class URLSearchParams
一些设置 url 上 ?xx=yy 也就是querystring的class
注:
对应的几个class
都是对应的,只不过针对的response,一般用不到,可能模拟测试的时候需要
- BaseResponseOptions
- Response
- ResponseOptions