在Angular 2中无法定义具有错误Handler中参数的构造函数



我正在尝试创建一个共享服务并从GlobalerRorhandler类中使用它来实现errorHandler,但是当我用参数编写构造函数时,我会遇到错误。

metadata_resolver.js:972 Uncaught SyntaxError {_nativeError: Error: Can't resolve all parameters for GlobalErrorHandler: (?).

globalerrorhandler

export class GlobalErrorHandler implements ErrorHandler{
    constructor(private errorService:ErrorService){
        //When i write this without parameters no syntax error
    } 
    handleError(error){
        alert(error);
        console.log("handeled by GlobalErrorHandler")
        console.log("error",error);
        console.log("******************");
        //this.errorService.emitGlobalError("msgTest");
        //instead of logging to the console, i would like to call globalModal component and show the error in a popup
    }
}

错误服务

import { Injectable } from '@angular/core';
import { Subject }    from 'rxjs/Subject';
@Injectable()
export class ErrorService {
    private globalErrorSource = new Subject<string>();
    globalErrorEmmitted= this.globalErrorSource.asObservable();
    emitGlobalError(msg){
            this.globalErrorSource.next(msg);
    }
}

appModule

@NgModule({
  imports: [
    BrowserModule,
    AppRoutingModule,
  ],
  declarations: [
    ListMediaChannel,
    CrudMediaChannel
  ],
  providers: [SharedService, MovieCategoryService, MediaChannelService,ErrorService,
    {
      provide: LocationStrategy,
      useClass: PathLocationStrategy
    },
    {provide:ErrorHandler,useClass:GlobalErrorHandler}
  ],
  bootstrap: [AppComponent]
})
export class AppModule { }

带有构造函数参数的服务类要求`@injectable()

@Injectable()
export class GlobalErrorHandler implements ErrorHandler{

在您的ErrorService上,它是可选的,因为它没有构造函数参数。

最新更新