在Angular 5中添加错误处理程序后无法实例化循环依赖性

时间:2018-08-23 08:27:58

标签: javascript angular typescript

尝试为Angular 5创建自定义错误处理程序以处理从Java收到的错误时出现JavaScript错误。

为此,我遵循了本教程:

https://itnext.io/how-to-handle-your-run-time-errors-in-your-angular-application-8d72fefbf8a4

但是我收到此错误:

Cannot instantiate cyclic dependency! ApplicationRef ("[ERROR ->]"): in NgModule App.module in ./AppModule@-1:-1

这是app.module.ts:

   import {ErrorHandler, NgModule} from '@angular/core';
import {BrowserModule} from '@angular/platform-browser';
import {FormsModule, ReactiveFormsModule} from '@angular/forms';
import {FooterComponent, LayoutModule, MainComponent, NavbarComponent} from './layout/index';
import {HomeModule} from './home/home.module';
import {HttpClient, HttpClientModule} from '@angular/common/http';
import {TranslateLoader, TranslateModule} from '@ngx-translate/core';
import {TranslateHttpLoader} from '@ngx-translate/http-loader';
import {KblEventManager} from './service/event-manager.service';
import {
  AddActionModule,
  KBL_BASEURL,
  KblHttpInterceptorModule,
  KblSpinnerComponent,
  KblSpinnerModule
} from '@kbl/components';
import {SlideinModule} from './slidein/slidein.module';
import {RouterModule} from '@angular/router';
import {APP_ROUTES} from './app.route';
import {BrowserAnimationsModule} from '@angular/platform-browser/animations';
import {SharedModule} from './shared/shared.module';
import {NgbModule} from '@ng-bootstrap/ng-bootstrap';
import {environment} from '../environments/environment';
import {HttpSpinnerComponent} from './layout/spinner/http-spinner.component';
import {AuthenticationService} from './model/authenticationService';
import {LoginService} from './login.service';
import {AppErrorHandlerService} from './app-error-handler.service';
import {RecommendationTableModule} from './recommendation-table/recommendation-table.module';
import {ReportingModule} from './reporting/reporting.module';
import {ReportModule} from './add-recommendation/report.module';
import {FollowUpModule} from './update-recommendation-dialog/follow-up.module';
import {MatSharedModule} from './shared/mat-shared.module';

const APP_BASE_URL

  = {provide: KBL_BASEURL, multi: true, useValue: environment.BASE_URL};

const APP_ERROR_HANDLER

  = {provide: ErrorHandler, useClass: AppErrorHandlerService};


export function createTranslateLoader(http: HttpClient) {
  return new TranslateHttpLoader(http, './assets/i18n/', '.json');
}

/*export function bootstrap(authentificationService: AuthenticationService): () => Promise<any> {
  return (): Promise<any> => {
    return authentificationService.initApp().toPromise();
  };
}*/

@NgModule({
  imports: [
    BrowserModule,
    LayoutModule,
    FormsModule,
    BrowserAnimationsModule,
    HomeModule,
    ReportingModule,
    ReportModule,
    FollowUpModule,
    RecommendationTableModule,
    RouterModule.forRoot(APP_ROUTES, {useHash: true}),
    TranslateModule.forRoot({
      loader: {
        provide: TranslateLoader,
        useFactory: (createTranslateLoader),
        deps: [HttpClient]
      }
    }),
    AddActionModule,
    SlideinModule,
    HttpClientModule,
    SharedModule,
    MatSharedModule,
    NgbModule,
    KblHttpInterceptorModule.forRoot(),
  ],

  declarations: [
    MainComponent,
    FooterComponent,
    NavbarComponent,
    // ManualAuthenticationDialogComponent,
    HttpSpinnerComponent,
  ],
  entryComponents: [
    KblSpinnerComponent,
  ],
  providers: [
    APP_ERROR_HANDLER,
    KblEventManager,
    APP_BASE_URL,
    AuthenticationService,
    LoginService],
  bootstrap: [MainComponent]
})
export class AppModule {
}

仅在添加ErrorHandler类后,我才收到此错误;如果我从提供程序中删除了该错误,则它可以正常运行,但没有异常处理程序。

这是我的错误堆栈:

[Error] Error: Provider parse errors:
Cannot instantiate cyclic dependency! ApplicationRef ("[ERROR ->]"): in NgModule AppModule in ./AppModule@-1:-1
    parse (compiler.js:19771)
    compile (compiler.js:20360)
    _compileModule (compiler.js:34658)
    compileModuleAsync (compiler.js:34481)
    bootstrapModule (core.js:5783)
    Codice Eval
    eval
    ../../../../../src/main/webapp/app/main.ts (main.bundle.js:281)
    __webpack_require__ (inline.bundle.js:55)
    (funzione anonima) (main.bundle.js:549)
    __webpack_require__ (inline.bundle.js:55)
    webpackJsonpCallback (inline.bundle.js:26)
    Codice globale (main.bundle.js:1)

这是AppErrorHandler.ts:

    import {ErrorHandler, Injectable} from '@angular/core';
import {BAD_REQUEST, FORBIDDEN, UNAUTHORIZED} from 'http-status-codes';
import {Router} from '@angular/router';
import {Toast, ToastsManager} from 'ng2-toastr';

@Injectable()
export class AppErrorHandlerService implements ErrorHandler {

  static readonly REFRESH_PAGE_ON_TOAST_CLICK_MESSAGE: string = 'An error occurred: Please click this message to refresh';
  static readonly DEFAULT_ERROR_TITLE: string = 'Something went wrong';

  constructor(private router: Router, private toastManager: ToastsManager) { }

  public handleError(error: any) {
    console.error(error);
    let httpErrorCode = error.httpErrorCode;
    switch (httpErrorCode) {
      case UNAUTHORIZED:
        this.router.navigateByUrl('/login');
        break;
      case FORBIDDEN:
        this.router.navigateByUrl('/unauthorized');
        break;
      case BAD_REQUEST:
        this.showError(error.message);
        break;
      default:
        this.showError(AppErrorHandlerService.REFRESH_PAGE_ON_TOAST_CLICK_MESSAGE);
    }
  }

  private showError(message: string) {
    this.toastManager.error(message, AppErrorHandlerService.DEFAULT_ERROR_TITLE, { dismiss: 'controlled'}).then((toast: Toast) => {
      let currentToastId: number = toast.id;
      this.toastManager.onClickToast().subscribe(clickedToast => {
        if (clickedToast.id === currentToastId) {
          this.toastManager.dismissToast(toast);
          window.location.reload();
        }
      });
    });
  }

}

0 个答案:

没有答案