Angular2 RC5 Default http headers

时间:2016-08-31 12:12:38

标签: angular

There was possibility to extend Http or BaseRequestOptions in RC4 and add default headers to all http requests. Like here How to set default HTTP header in Angular2? How to do it in RC5 ?

Found export declare function httpFactory(xhrBackend: XHRBackend, requestOptions: RequestOptions): Http; in http.d.ts line 163. Maybe it can help.

1 个答案:

答案 0 :(得分:0)

You still can use CustomRequestOptions just like in RC4:

@NgModule({
  imports: [...],
  providers: [provide(RequestOptions, { useClass: CustomRequestOptions}) ],
  declarations: [AppComponent],
  bootstrap: [AppComponent]
})


import { Headers, RequestOptions, RequestOptionsArgs } from '@angular/http';

export class CustomRequestOptions extends RequestOptions {
    merge(options?: RequestOptionsArgs): RequestOptions {
            let headers = new Headers({ 'Content-Type': 'application/json' });
            options.headers = headers;
            var result = super.merge(options);
            result.merge = this.merge;
            return result;
        }
        return super.merge(options);
    }
}