Angular2:捕捉角异常 [英] Angular2: Catch angular exceptions

查看:1283
本文介绍了Angular2:捕捉角异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果Angular2遇到内部异常不会被记录到控制台。我如何检测这些异常类似以下?

If Angular2 encounters an internal exception it won't be logged to the console. How can I detect such exceptions like the following one?

EXCEPTION: Error during instantiation of MainFormComponent!.
ORIGINAL EXCEPTION: TypeError: Cannot set property 'crashMeMaybe' of undefined
ORIGINAL STACKTRACE:
... stacktrace ...
ERROR CONTEXT:
... context object ...

是否有任何可用的预订?哪有这样的文件?

Are there any available subscriptions? Where is such documentation?

推荐答案

有是界面的ExceptionHandler,通过它可以提供您所选择的自定义实现。请参见 href=\"http://stackoverflow.com/questions/32488245/custom-exception-handler-in-angular2\">和的文档

There is interface ExceptionHandler through which you can provide custom implementation of your choosing. See here and documentation.

从BETA.0来源:(正面/ exception_handler.d.ts)

From BETA.0 sources: (facade/exception_handler.d.ts)

/**
 * Provides a hook for centralized exception handling.
 *
 * The default implementation of `ExceptionHandler` prints error messages to the `Console`. To
 * intercept error handling,
 * write a custom exception handler that replaces this default as appropriate for your app.
 *
 * ### Example
 *
 * ```javascript
 *
 * class MyExceptionHandler implements ExceptionHandler {
 *   call(error, stackTrace = null, reason = null) {
 *     // do something with the exception
 *   }
 * }
 *
 * bootstrap(MyApp, [provide(ExceptionHandler, {useClass:    MyExceptionHandler})])
 *
 * ```
 */

更新:

我有BETA.0一个小问题。

I had a little problem with BETA.0


  • 进口已被黑客攻破,

  • 界面似乎与内部细节被污染。

我已经运行它像:

import {ExceptionHandler} from 'angular2/src/facade/exception_handler';

export interface IExceptionHandler {
    call(exception: any, stackTrace?: any, reason?: string): void;
}

export class CustomExceptionHandler implements IExceptionHandler {
    call(exception: any, stackTrace: any, reason: string): void {
        alert(exception);
    }
}

bootstrap(DemoApp, [
    new Provider(ExceptionHandler, {
        useClass: CustomExceptionHandler
    })
]);

这篇关于Angular2:捕捉角异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆