我正在尝试创建一个共享服务并从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 { }
答案 0 :(得分:2)
带有构造函数参数的服务类需要`@Injectable()
@Injectable()
export class GlobalErrorHandler implements ErrorHandler{
在ErrorService
它是可选的,因为它没有构造函数参数。