I have this global error handler:
import { ErrorHandler, Injectable, Injector } from '@angular/core';
import { Router } from '@angular/router';
@Injectable()
export class GlobalErrorHandler extends ErrorHandler
{
constructor(private injector: Injector) {
super(false);
}
handleError(error) {
super.handleError(error);
const router = this.injector.get(Router);
router.navigateByUrl('/error');
}
}
I put it in app.module:
providers: [
{
provide: ErrorHandler,
useClass: GlobalErrorHandler
}
]
to test this I throw an error inside a service. For some reason the error component shows up together with the other contents of the page. Something like this:
if I test the error component by going to /error it shows up like this (the error page is a paragraph with a single line):
Sorry, something went wrong!
but when I go to my home page where the error should trigger it shows up like this:
Sorry, something went wrong!
....
... Original content for /home
....
Sorry, something went wrong!
Can someone enlighten me what am I doing wrong?
Update
I've updated to angular-cli 1.5.2 AND also updated to angular 5 and the bug seems fixed there. I can navigate to the error page using the router.