Change in Angular2 RC5 ExceptionHandler?

江枫思渺然 提交于 2019-12-12 21:14: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 ]
})


来源:https://stackoverflow.com/questions/38882969/change-in-angular2-rc5-exceptionhandler

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!