2016-09-29 96 views
2

我創建了一個自定義的Http提供程序來處理我們的應用程序中的一個地方的http錯誤。在RC4我用來引導應用程序,像這樣來實現這一目標:Angular2提供覆蓋類

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中使用提供嗎?

謝謝!

回答

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 {} 
+1

你回答非常類似的問題對我來說:http://stackoverflow.com/questions/39286983/how-to-override-http-class-in-rc6 –

+0

再次感謝您!這是唯一不得不改變的是ngModule的屬性是提供者而不是提供者。 – cobolstinks

+0

對不起,感謝您的反饋:) –