遇到异常后,应用程序无响应 [英] Application becomes unresponsive after encountering an exception

查看:90
本文介绍了遇到异常后,应用程序无响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当遇到异常时,如何告诉Angular 2不要阻止整个应用程序?

How to tell Angular 2 to not block the whole application when it encounters an exception?

我不确定是否有可能,因为Google对此没有启发. 但是对于任何单页Web应用程序来说,这似乎都是至关重要的.

I'm not sure if it's even possible, because Google didn't enlighten me on this. But it seems critical to any single page web application.

每当Angular 2遇到异常并引发错误时,整个应用程序都将无响应.

Whenever Angular 2 encounters an exception and throws an error, the complete application becomes unresponsive.

我知道在JavaScript中

I know that in JavaScript

try {
  doSomething();
}
catch(err) {
  handleErrors(err);
}

可能会解决问题.

但是我只想知道是否有任何特定于Angular的解决方案或解决方法?

But I just want to know if there is any Angular specific solution or workaround?

推荐答案

在angular 2最终版本上,您可以实现自定义ErrorHandler(

On angular 2 final version, you can implement custom ErrorHandler (Angular 2 docs example):

import {NgModule, ErrorHandler} from '@angular/core';

class MyErrorHandler implements ErrorHandler {
  handleError(error) {
    // do something with the exception
  }
}

@NgModule({
  providers: [{provide: ErrorHandler, useClass: MyErrorHandler}]
})
class MyModule {}

这篇关于遇到异常后,应用程序无响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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