Home:ALL Converter>RequestOptions migration Angular 5

RequestOptions migration Angular 5

Ask Time:2017-12-01T10:21:32         Author:Abner

Json Formatter

I was using a custom request options in Angular 4 where I was doing the following:

default-request-options.service.ts

@Injectable()
export class DefaultRequestOptions extends BaseRequestOptions {
  headers = new Headers({
    'Accept': 'application/json',
    'Content-Type': 'application/json'
  });

  merge(options?: RequestOptionsArgs): RequestOptions {
    var newOptions = super.merge(options);
    const token: string = localStorage.getItem('token');
    if (token) {
      newOptions.headers.set('token', token);
    }
    return newOptions;
  }
}

App.Module.ts

providers: [ // expose our Services and Providers into Angular's dependency injection
    { provide: RequestOptions, useClass: DefaultRequestOptions }
  ]

But after the migration notice that the RequestOption is not available in the new folder http/common/http

I'm would like to know if I still can use similar thing in Angular 5 or there is no point using it with the new HTTPClient? The main advantage for me was to set in one place only, not having to append it to all my requests.

I got the code initially in the angular docs: https://github.com/angular/angular.io/blob/master/public/docs/_examples/server-communication/ts/src/app/default-request-options.service.ts

Author:Abner,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/47585720/requestoptions-migration-angular-5
yy