rematnarab
rematnarab

Reputation: 1325

Can not define a constructor with parameters in ErrorHandler in angular 2

I am trying to create a shared service and use it from GlobalErrorHandler class which implements ErrorHandler but i get an error when i write the constructor with parameters.

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
    }

}

Error Service

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 { }

Upvotes: 3

Views: 1070

Answers (1)

G&#252;nter Z&#246;chbauer
G&#252;nter Z&#246;chbauer

Reputation: 658067

Service classes with constructor parameters require `@Injectable()

@Injectable()
export class GlobalErrorHandler implements ErrorHandler{

On your ErrorService it's optional, because it doesn't have constructor parameters.

Upvotes: 4

Related Questions