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

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

问题描述

如果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,您可以通过它提供您选择的自定义实现。请参见此处文档

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

从BETA.0来源: (facade / 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
    })
]);

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

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