Change in Angular2 RC5 ExceptionHandler?

2019-07-16 01:05发布

问题:

I'm migrating my Angular2 code to RC5 and can't figure out how to wire up my exception handling. In the RC4, it was part of the bootstrapping process in Main.ts:

bootstrap(MyApp, [{provide: ExceptionHandler, useClass: GlobalExceptionHandler}])

But now that we have an app.module.ts, I'm not sure how to include the reference to ExceptionHandler and point it at GlobalErrorHandler. My new app.module.ts is below:

import { NgModule } from "@angular/core";
import { BrowserModule  } from "@angular/platform-browser";
import { FormsModule } from "@angular/forms";
import { AppComponent }   from "./app.component";
import { ExceptionHandler } from "@angular/core";
import { GlobalErrorHandler } from "./shared/utils/global-error-handler";
import { ExceptionApiService } from "./shared/utils/exception-api-service";
import { HomeComponent }   from "./home/home.component";
import { ContentManagementComponent } from "./contentManagement/content-management.component";
import { GetContentComponent } from "./getContent/get-content.component";
import { AddContentComponent } from "./addContent/add-content.component";
import { ErrorComponent } from "./errorPage/error.component";
import { appRoutingProviders, APP_ROUTER_PROVIDERS } from "./app.routes";
import { APP_PROVIDERS } from "./app.providers";

@NgModule({
    imports:      [ BrowserModule, FormsModule, APP_ROUTER_PROVIDERS ],
    declarations: [ AppComponent, HomeComponent,     ContentManagementComponent, GetContentComponent, AddContentComponent, ErrorComponent ],
    providers:    [ appRoutingProviders, APP_PROVIDERS, GlobalErrorHandler, ExceptionApiService ],
    bootstrap:    [ AppComponent ]
})

// How to bootstrap ExceptionHandler and point at GlobalErrorHandler??

export class AppModule {}

Any ideas? Not a whole lot of documentation out there related to ExceptionHandling...especially for RC5 :)

回答1:

Similar to other providers, you need to inject it into your module.

@NgModule({
    imports:      [ BrowserModule, FormsModule, APP_ROUTER_PROVIDERS ],
    declarations: [ AppComponent, HomeComponent,     ContentManagementComponent, GetContentComponent, AddContentComponent, ErrorComponent ],
    providers:    [ appRoutingProviders, APP_PROVIDERS, {provide: ExceptionHandler, useClass: GlobalExceptionHandler}, ExceptionApiService ],
    bootstrap:    [ AppComponent ]
})