简体   繁体   English

Angular 6 错误处理不显示吐司

[英]Angular 6 Error Handling not showing toasts

I am using the code below to catch errors in my small Angular 6 app... I am catching the errors, I am showing info in the console the problem is that the toasts do not show unless I click somewhere in the application, they don't show up all by themselves.我正在使用下面的代码来捕获我的小型 Angular 6 应用程序中的错误......我正在捕获错误,我正在控制台中显示信息问题是除非我单击应用程序中的某个位置,否则不会显示 toasts,它们不会显示不会自己一个人出现。 I am using the ToastrService in other parts of the application and whenever I call it like I do it here it shows toats without having to click on anything.我在应用程序的其他部分使用 ToastrService,每当我像在这里那样调用它时,它都会显示 toats,而无需单击任何东西。

Any idea what might be causing this behaviour?知道什么可能导致这种行为吗?

import { Injectable, ErrorHandler, Injector } from '@angular/core';
import { Router } from '@angular/router';
import { HttpErrorResponse } from '@angular/common/http';
import { ToastrService } from 'ngx-toastr';
import { AppSettings } from '../../app.settings';

@Injectable()
export class GlobalErrorHandler implements ErrorHandler {

  constructor(private injector: Injector) {}

  public handleError(error: any) {

    const router = this.injector.get(Router);
    const settings = this.injector.get(AppSettings)

    const toastr = this.injector.get(ToastrService);

    console.log(`Request URL: ${router.url}`);
    console.log(error);
    if (error instanceof HttpErrorResponse) {
      console.log("it is");
      if (error.status === 401) {
        router.navigate(['/login']);
        settings.settings.setLoadingSpinner(false);
        toastr.error('Logged in user no longer authenticated on server.', 'Unable to connect to server');
      } else if (error.status === 404) {
        console.log("it is 404");
        toastr.error('Unable to connect to server. Missing or wrong URL, please try again', 'Unable to connect to server');
        settings.settings.setLoadingSpinner(false);
      } else if (error.status === 0) {
        toastr.error('Server appears to be temporary unavailable', 'Unable to connect to server');
        settings.settings.setLoadingSpinner(false);
      } else if (error.status === 500) {
        toastr.error('Server appears to be temporary unavailable', 'Unable to connect to server');
        settings.settings.setLoadingSpinner(false);
      }

    } else {
      console.error(error);
      toastr.error('An error has occured', 'Sorry');

    }

  }

}

I have added the provider in the module also to use this class as error handler.我还在模块中添加了提供程序,以将此类用作错误处理程序。

please include the toaster service in constructor and run.请在构造函数中包含烤面包机服务并运行。 As per my understanding, while service get instantiated, toaster should have to get initiated.根据我的理解,当服务被实例化时,烤面包机应该被启动。

I believe you can import and include NgZone in your constructor to have this work as intended:我相信您可以在构造函数中导入并包含NgZone以按预期进行这项工作:

constructor(private injector: Injector, private zone: NgZone) {}

Then wrap your toastr calls:然后包装您的toastr调用:

this.zone.run(() => toastr.error('message', 'title'));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM