Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RequestOptions migration Angular 5

Tags:

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