【问题标题】:Display a nice div when http error code 500 was caught捕获 http 错误代码 500 时显示一个漂亮的 div
【发布时间】:2018-01-16 22:25:02
【问题描述】:

我创建了一个简单的ErrorHandler 类,它看起来:

export class AppErrorHandler implements ErrorHandler {

    handleError(error : any){
        //console.log('test error ', error.status);
        if(error.orginalError.status === 500){
            alert('An unexpected error occured 500');
            console.log('from AppErrorHandler ', error);
        }
        else {
            alert('An unexpected error occured');
            console.log('from AppErrorHandler ', error);
        }   
    }
}

如您所见,我只在发生错误时在页面上显示alert。经过一段时间的挖掘,我还没有找到在任何类型的div 中使用引导类alert alert-danger 显示该错误的解决方案。一般可以吗?

【问题讨论】:

    标签: html angular error-handling


    【解决方案1】:

    如果你想使用 3rd 方库,有一个很棒的包 "angular2-notifications" 可以显示通知。

    这里有一个链接:

    https://www.npmjs.com/package/angular2-notifications

    ...您可以在此处找到文档:

    https://jaspero.co/resources/projects/ng-notifications

    ...当然还有带有示例的源代码:

    https://github.com/flauc/angular2-notifications

    【讨论】:

      【解决方案2】:

      我认为你应该这样做:

      • 创建包含错误的服务
      • 在您的错误处理程序中,将错误添加到 errorService
      • 创建一个组件,它将成为您的“好 div”
      • 将此组件放置在您的AppComponent 中,以便它始终可见。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2010-12-03
        • 2019-08-24
        • 2017-06-20
        • 2019-09-04
        • 1970-01-01
        • 2015-05-15
        • 1970-01-01
        相关资源
        最近更新 更多