cobolstinks
cobolstinks

Reputation: 7153

Angular2 Provide to Override Class

I created a custom Http provider to handle http errors in one place in our app. In RC4 i used to bootstrap the app like so to achieve this:

bootstrap(AppComponent, [
    APP_ROUTER_PROVIDERS,
    HTTP_PROVIDERS,
    provide(HttpErrorService, { useClass: HttpErrorService }),  
    provide(LocationStrategy, { useClass: HashLocationStrategy }),
    provide(PLATFORM_DIRECTIVES, { useValue: [ROUTER_DIRECTIVES], multi: true }),
    provide(Http, {
        useFactory: (xhrBackend: XHRBackend, requestOptions: RequestOptions, eService: HttpErrorService) => new CustomHttp(xhrBackend, requestOptions, eService),
        deps: [XHRBackend, RequestOptions, HttpErrorService]
    })
]);

How do I do something similar now that I'm running the release version? Can i use provide in my main app.module somehow?

Thanks!

Upvotes: 2

Views: 670

Answers (1)

Günter Zöchbauer
Günter Zöchbauer

Reputation: 657741

@NgModule({
  bootstrap: [AppComponent],
  directives: [AppComponent],
  imports: [BrowserModule, HttpModule, routing],
  providers: [
    {provide: HttpErrorService, useClass: HttpErrorService },  
    {provide: LocationStrategy,  useClass: HashLocationStrategy },
    {provide: Http, 
        useFactory: (xhrBackend: XHRBackend, requestOptions: RequestOptions, eService: HttpErrorService) => new CustomHttp(xhrBackend, requestOptions, eService),
        deps: [XHRBackend, RequestOptions, HttpErrorService]
    }]
])
export class AppModule {}

Upvotes: 2

Related Questions