2
I’m looking for a way to intercept all HTTP requests from the angle and add a few headers. In versions prior to angular2 RC5
(before the NgModule
) was that way, for example:
class MyOptions extends BaseRequestOptions {
Authorization: string = 'Bearer ' + localStorage.getItem('tokenName');
}
bootstrap(AppComponent,
[HTTP_PROVIDERS,
{ provide: RequestOptions, useClass: MyOptions },
appRouterProviders,
disableDeprecatedForms(),
provideForms(),
]).catch(err => console.error(err));
I’m currently in the version 2.0 final
and as per research on how this would be implemented in this version, would be something similar to this:
@NgModule({
imports: [ BrowserModule ],
declarations: [ AppComponent ],
providers: [
{ provide: RequestOptions, useClass: MyOptions }
],
bootstrap: [ AppComponent ]
})
export class AppModule {
}
@Injectable()
export class MyOptions extends RequestOptions {
constructor() { super({method: RequestMethod.Get, headers: new Headers()}); }
}
which presents the following error:: TypeError: Cannot read property 'merge' of null
. As can be seen in this example.
Note: The implementation of
MyOptions
is the same asBaseRequestOptions
, I copied it so if you useBaseRequestOptions
in{ provide: RequestOptions, useClass: BaseRequestOptions}
, everything works, as can be observed in this example.