As shown below:
//Scheme 1: Can be used, but token information cannot be written in the service. It should be replaced by global variables, and parameter compounding issues must also be considered.@Injectable() export class DefaultRequestOptions extends BaseRequestOptions { constructor() { super(); ('Content-Type', 'application/json'); } } export const requestOptionProvider = {provide: RequestOptions, useClass: DefaultRequestOptions}; //Scheme 2: It can be used, it is a relatively formal implementation, but in the future, you can only use myHttp object, and you must also consider the parameter compounding issue.export class myHttp extends Http { defaultHeaders = new Headers({ 'Content-Type': 'application/json' }); constructor(_backend: ConnectionBackend, _defaultOptions: RequestOptions, private appInitService: AppInitService) { super(_backend, _defaultOptions); } get(url: string, options?: RequestOptionsArgs): Observable<Response> { let optionBuf = new RequestOptions({ headers: , params: {'token': } }); //here extend options; return (url, optionBuf); } } export function myHttpFactory(xhrBackend: XHRBackend, requestOptions: RequestOptions, appInitService: AppInitService): myHttp { return new myHttp(xhrBackend, requestOptions, appInitService); // Create myHttp object} export const myHttpProvider = { provide: myHttp, useFactory: myHttpFactory, deps: [XHRBackend, RequestOptions, AppInitService] };
The above article angular2's unified http request header is all the content I share with you. I hope you can give you a reference and I hope you support me more.