Angular2提供覆盖类

时间:2016-09-29 14:06:41

标签: angular angular2-di

我创建了一个自定义Http提供程序来在我们的应用程序中的一个位置处理http错误。在RC4中,我曾经像这样引导应用程序来实现这个目标:

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]
    })
]);

现在我正在运行发布版本,我该如何做类似的事情?我能以某种方式在我的主app.module中使用提供吗?

谢谢!

1 个答案:

答案 0 :(得分:2)

@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 {}