Angular:仅在构建生产版本时出现模块未找到错误

3

更新

在升级到Angular v4后,我遇到了以下错误(没有更改过代码)。

ERROR in Invalid provider for ErrorHandler in /Users/otusweb/Documents/KeynoteTweet/web/node_modules/@angular/core/core.d.ts. useClass cannot be null.
       Usually it happens when:
       1. There's a circular dependency (might be caused by using index.ts (barrel) files).
       2. Class was used before it was declared. Use forwardRef in this case.

ERROR in ./src/main.ts
Module not found: Error: Can't resolve './$$_gendir/app/app.module.ngfactory' in '/Users/otusweb/Documents/KeynoteTweet/web/src'
 @ ./src/main.ts 4:0-74
 @ multi ./src/main.ts

原问题

我正在尝试在我的Angular 2应用程序中设置自定义全局错误处理程序以将这些错误发送到Rollbar。 我遵循了 https://www.bennadel.com/blog/3138-creating-a-custom-errorhandler-in-angular-2-rc-6.htmhttps://netbasal.com/angular-2-custom-exception-handler-1bcbc45c3230 的步骤。

在开发模式下运行一切正常。 一旦我构建生产版本(ng build --prod),我就会在浏览器中收到以下错误:

Error: No ErrorHandler. Is platform module (BrowserModule) included?

这里是代码: error-handling.ts
import { RollbarService } from  'angular-rollbar/lib';
import { ErrorHandler } from '@angular/core';
import { Inject } from "@angular/core";
import { Injectable } from "@angular/core";
import { isDevMode } from '@angular/core';

@Injectable()
export default class CustomErrorHandler extends ErrorHandler {
  private rollbar:RollbarService;

  constructor(rollbar: RollbarService) {
    // The true paramter tells Angular to rethrow exceptions, so operations like 'bootstrap' will result in an error
    // when an error happens. If we do not rethrow, bootstrap will always succeed.
    super(true);

    this.rollbar = rollbar;
  }

  handleError( error: any ) : void {
    if(!isDevMode()) 
    {
    // send the error to the server
    this.rollbar.error(error.message, error);
    }
    else
    {
    console.log("skipping rollbar");
    }

    // delegate to the default handler
    super.handleError(error); 
  }
}

以及app.module(不包括导入)

    @NgModule({
  declarations: [
      AppComponent,
      PresentationsComponent,
      PresentationDetailComponent,
      SlideDetailComponent,
      HomeComponent,
      NotFoundComponent
  ],
  imports: [ 
      BrowserModule,
      FormsModule,
      HttpModule,
      AppRoutingModule,
      InlineEditorModule,
      RollbarModule.forRoot({
        accessToken: 'xxxxxtokenxxxx',
      }),
      NgbModule.forRoot()
  ],
  providers: [
    { provide: ErrorHandler, useClass: CustomErrorHandler },
    PresentationService,
    SlideService,
    Auth,
    {
      provide: AuthHttp,
      useFactory: authHttpServiceFactory,
      deps: [ Http, RequestOptions ]
    },
  AuthGuard,
  LoggedOutGuard],
  bootstrap: [AppComponent]
})

我错过了什么?
3个回答

6
发现问题了,在我的自定义错误处理程序的应用程序模块中,导入语句缺少括号...
import  CustomErrorHandler  from './error-handling';

与其

import { CustomErrorHandler } from './error-handling';

Angular 8.0.0,ng build --prod由于某些原因无法工作 :) 谢谢你的帮助。 - moolsbytheway

1

我遇到了完全相同的问题,但是添加大括号后又出现了另一个错误。除了添加大括号外,我还需要在 error-handling.ts 中删除 'default':

更改为:

 @Injectable()
 export default class CustomErrorHandler extends ErrorHandler {

由:

 @Injectable()
 export class CustomErrorHandler extends ErrorHandler {

并添加大括号:

import { CustomErrorHandler } from './error-handling';

0

看起来你的一个子模块找不到BrowserModule,尝试将其添加到导出项中:

exports: [
  BrowserModule,
]

谢谢您的建议。我刚试了一下,但是没有成功。 - otusweb

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接