Topic: ToastService
gvonhagel pro asked 6 years ago
israelfreitas pro answered 4 years ago
Hi!
Finally, I found the solution! Thanks to michelepatrassi.
My ErrorHandler now looks like this:
import { ErrorHandler, Injectable, Injector, NgZone } from '@angular/core';
import { ToastService } from 'ng-uikit-pro-standard';
@Injectable()
export class MyErrorHandler implements ErrorHandler {
constructor(private readonly injector: Injector, private readonly ngZone: NgZone) { }
private get toast() { return this.injector.get(ToastService) }
handleError = (error: any): void => {
this.ngZone.run(() => this.toast.error('Some message here...', 'Error!', {}))
}
}
From now, the ErrorHandler knows how to handle errors from Observables.
israelfreitas pro answered 4 years ago
Hi.
I'd like to know how that problem was fixed.
I have the same scenario (global error handler + Injector's injection to avoid cyclic dependency error) and I imported the ToastModule once using ToastModule.forRoot(), but I get the same error (a blank alert when using toast service as this.injector.get(ToastService)).
I'm using the latest version of MBD Angular.
Thank you.
israelfreitas pro commented 4 years ago
After some tests, I realize that it seems to be an issue with RxJS Observables. Look:
// test.component.ts:
throwErrorString() { // ErrorHandler captures the error and shows the toast just fine.
throw "An error...";
}
throwErrorObject() { // ErrorHandler captures the error and shows the toast just fine.
throw new Error("An error...");
}
throwRxJSThrowError() { // ErrorHandler capture the error but shows the a crashed toast.
throwError("An error...").subscribe();
}
Any help is appreciated.Thank you.
Dawid Adach pro answered 6 years ago
gvonhagel pro commented 6 years ago
I am using version 5.1.2, double checked to be sure. I installed MDB PRO via NPM. I am only importing the ToastModule once using ToastModule.forRoot() as specified in the documentation, and injecting it in the exception handling module as ToastService.FREE CONSULTATION
Hire our experts to build a dedicated project. We'll analyze your business requirements, for free.
Answered
- ForumUser: Pro
- Premium support: No
- Technology: MDB Angular
- MDB Version: -
- Device: -
- Browser: -
- OS: -
- Provided sample code: No
- Provided link: No
gvonhagel pro commented 6 years ago
Anyone? I know you guys have a lot of questions to get to, but a simple point in the right direction would be great.gvonhagel pro commented 6 years ago
Anyone..?