Angular2 - Class - Request

class RequestOptions – 创建request的参数
class BaseRequestOptions – 具有一些默认值的经典参数

RequestOptions

import {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

注意merge的说明。不覆盖的

constructor({method, headers, body, url, search}: RequestOptionsArgs = {}) {
this.method = isPresent(method) ? normalizeMethodName(method) : null;
this.headers = isPresent(headers) ? headers : null;
this.body = isPresent(body) ? body : null;
this.url = isPresent(url) ? url : null;
this.search = isPresent(search) ? (isString(search) ? new URLSearchParams(<string>(search)) :
<URLSearchParams>(search)) :
null;
}

注意着3个的类型

headers: Headers;
method: RequestMethod | string;
search: URLSearchParams;

BaseRequestOptions

Default values:

  • method: RequestMethod.Get
  • headers: empty Headers object

覆盖默认的BaseRequestOptions

import {provide} from 'angular2/core';
import {bootstrap} from 'angular2/platform/browser';
import {HTTP_PROVIDERS, Http, BaseRequestOptions, RequestOptions} from 'angular2/http';
import {App} from './myapp';

class MyOptions extends BaseRequestOptions {
search: string = 'coreTeam=true';
}

bootstrap(App, [HTTP_PROVIDERS, provide(RequestOptions, {useClass: MyOptions})]);

Request

看下构造

constructor(requestOptions: RequestArgs) {
// TODO: assert that url is present
let url = requestOptions.url;
this.url = requestOptions.url;
if (isPresent(requestOptions.search)) {
let search = requestOptions.search.toString();
if (search.length > 0) {
let prefix = '?';
if (StringWrapper.contains(this.url, '?')) {
prefix = (this.url[this.url.length - 1] == '&') ? '' : '&';
}
// TODO: just delete search-query-looking string in url?
this.url = url + prefix + search;
}
}
this._body = requestOptions.body;
this.method = normalizeMethodName(requestOptions.method);
// TODO(jeffbcross): implement behavior
// Defaults to 'omit', consistent with browser
// TODO(jeffbcross): implement behavior
this.headers = new Headers(requestOptions.headers);
}

发出自己的request

import {Injectable, Injector} from 'angular2/core';
import {HTTP_PROVIDERS, Http, Request, RequestMethod} from 'angular2/http';

@Injectable()
class AutoAuthenticator {
constructor(public http:Http) {}
request(url:string) {
return this.http.request(new Request({
method: RequestMethod.Get,
url: url,
search: 'password=123'
}));
}
}

var injector = Injector.resolveAndCreate([HTTP_PROVIDERS, AutoAuthenticator]);
var authenticator = injector.get(AutoAuthenticator);
authenticator.request('people.json').subscribe(res => {
//URL should have included '?password=123'
console.log('people', res.json());
});

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