1

我正在尝试在 Angular 2 中实现一个中央 ExceptionHandler。我在 SO 中进行了搜索,发现了几个关于这个主题的主题,如下所示:

模块 '"angular2/angular2"' 没有导出的成员 'ExceptionHandler'

如何正确覆盖angularjs中的exceptionHandler?

但是,这个话题似乎已经过时了。

我正在使用 Angular 2 RC5,并按照此处的文档,我尝试实现:

import { ExceptionHandler } from '@angular/core';
export class CustomExceptionHandler implements ExceptionHandler {

call(error:any, stackTrace:any = null, reason:string = null) {
    // do something with the exception
}

}

但我收到类似“TS2420 - CustomExceptionHandler 错误地实现接口 ExceptionHandler。‘CustomExceptionHandler’ 类型中缺少属性‘_logger’。

我是 TS 的新手,但我已经用 Java 编程了 10 年。

实际上,当我在 IDE (IntelliJ) 中单击 ExceptionHandler 时,代码会跟随一个类,而不是一个接口。

export declare class ExceptionHandler {
    private _logger;
    private _rethrowException;
    constructor(_logger: any, _rethrowException?: boolean);
    static exceptionToString(exception: any, stackTrace?: any, reason?: string): string;
    call(exception: any, stackTrace?: any, reason?: string): void;
}
4

1 回答 1

3

下面是RC6,

Exceptions 已弃用供公众使用,请参阅 RC6 的更改日志

核心:异常不再是公共 API 的一部分。我们不希望任何人提到异常类型。

ExceptionHandler.call(exception: any, stackTrace?: any, reason?: string): void;

改成:

ErrorHandler.handleError(error: any): void;

有关使用的实现ErrorHandler请参阅此

class MyErrorHandler implements ErrorHandler {
  call(error, stackTrace = null, reason = null) {
    // do something with the exception
  }
}
@NgModule({
  providers: [{provide: ErrorHandler, useClass: MyErrorHandler}]
})
class MyModule {}

希望这可以帮助!!

于 2016-09-06T18:22:27.707 回答