Angular 当错误通过CustomErrorHandler时显示自定义消息

Angular 当错误通过CustomErrorHandler时显示自定义消息,angular,error-handling,http-status-code-404,Angular,Error Handling,Http Status Code 404,我想创建一个自定义弹出窗口,在出现任何http错误时通知用户并显示自定义消息。为此,我创建了一个自定义事件处理程序,如下所示: @Injectable() export class CustomErrorHandler extends ErrorHandler { constructor(private errorHandlerService: ErrorHandlerService){ super(false); } public handleError(error: a

我想创建一个自定义弹出窗口,在出现任何http错误时通知用户并显示自定义消息。为此,我创建了一个自定义事件处理程序,如下所示:

@Injectable()
export class CustomErrorHandler extends ErrorHandler {

  constructor(private errorHandlerService: ErrorHandlerService){
    super(false);
  }
  public handleError(error: any): void {
    super.handleError(error);
    if (error.status == 500){
      this.errorHandlerService.setStatus = error.status;
      alert('error 500');
    }
  }
}
此外,我还创建了一个只有一个属性-状态的服务:

这是一个保持当前错误状态的类。通过getStatus方法,我将确定是否需要显示消息。当然,我已经在app.module中添加了提供者部分的必要字段

在app.component.html中-我添加的主要组件

<error-handler></error-handler>
app.component.html

通过将添加到此文件,其将始终全局可用

错误处理程序容器
使用什么将组件添加到app.component.html?并跟踪app.component.tsI不理解您订阅的服务是否存在错误。请提供一些示例不幸的是,此解决方案不起作用。我在app.component.ts->ngOnInit{this.errorHandlerService.getStatus.subscribeerr:any=>{console.log'status:',err;if err==500{this.shourrormessage=true;}else{this.shourrormessage=false;};}如果出现错误,错误总是未定义的。如何设置状态?你把它放在哪里?你能发布一段吗?这里:public-handleErrorerror:any:void{//你可以在这里添加自己的逻辑//不需要委托给原始实现super.handleErrorerror;if-error.status==500{this.errorHandlerService.setStatus=error.status;alert'error 500';}在扩展ErrorHandler的custromErrorHandler中,您不能再像这样设置值,请改用此方式。errorHadlerService.setStatus error.status;在添加http拦截器之后,它将
<error-handler></error-handler>
@Component({
  selector: 'error-handler',
  templateUrl: './error-handler.component.html',
  styleUrls: ['./error-handler.component.css']
})
export class ErrorHandlerComponent implements OnInit {
  showErrorMessage = false;

  constructor(private errorHandlerService: ErrorHandlerService) {}

  ngOnInit() {}

}

.html
<h1 *ngIf="showErrorMessage ">Error appears!</h1>