在Angular2定义异常处理程序

在Angular2定义异常处理程序

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

问题描述

在angular2例外记录在默认情况下,控制台。我听说我们可以继承角的ExceptionHandler,创造我们自己的异常处理程序,使我们可以覆盖默认行为。我试着做,但没有奏效。谁能帮我在这一个例子。在此先感谢....

In angular2 the exceptions are logged in the console by default. I heard that we can inherit the Angular ExceptionHandler and create our own exception handler so that we can override the default behavior. I tried to do it but didn't work. Can anyone help me on this with an example. Thanks in advance ....

推荐答案

看起来你需要创建自己的类来处理异常,然后在您的应用程序在引导时绑定的,是这样的:

It looks like you need to create your own class to handle exceptions, and then bind it in your app at bootstrap time, something like this:

class MyExceptionHandler implements ExceptionHandler {
    call(error, stackTrace = null, reason = null) {
       // do something with the exception
    }
 }

然后在引导时,绑定这个新的实施作为的ExceptionHandler:

And then at bootstrap time, bind this new implementation as the ExceptionHandler:

bootstrap(MyApp, [bind(ExceptionHandler).toClass(MyExceptionHandler)])

请参阅以供参考。

这篇关于在Angular2定义异常处理程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 03:45