gpt4 book ai didi

angular - 无法在 Angular 2 的 ErrorHandler 中定义带有参数的构造函数

转载 作者:太空狗 更新时间:2023-10-29 18:30:35 24 4
gpt4 key购买 nike

我正在尝试创建一个共享服务并从实现了 ErrorHandler 的 GlobalErrorHandler 类中使用它,但是当我使用参数编写构造函数时出现错误。

metadata_resolver.js:972 Uncaught SyntaxError {_nativeError: Error: Can't resolve all parameters for 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);
}
}

应用模块

@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 上它是可选的,因为它没有构造函数参数。

关于angular - 无法在 Angular 2 的 ErrorHandler 中定义带有参数的构造函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46368220/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com