Angular 4.
I am trying to make an error page to show some information about an unhandled exception that might occur. GlobalErrorHandler
intercepts possible errors and redirects the user to a page consisting of one ErrorComponent
. When an error occurs, the page is displayed, but lifecycle hooks are not called.
ErrorHandler :
@Injectable()
export class GlobalErrorHandler extends ErrorHandler {
constructor(
private injector: Injector
) {
super(true);
}
handleError(error: any) {
const router = this.injector.get(Router);
if (!router.url.startsWith('/error')) {
router.navigate(['/error']);
}
super.handleError(error);
}
}
ErrorComponent :
@Component({
selector: 'error-desc',
template: '<h1>Error page = {{code}}</h1>'
changeDetection: ChangeDetectionStrategy.OnPush
})
export class ErrorComponent implements OnInit {
public code: string = '';
constructor(
) {}
ngOnInit() {
this.code="AAAA";
console.log("OnInit");
}
ngOnDestroy() {
console.log("OnDestroy");
}
}
Working demo on plunkr .
How can i fix this? Maybe someone knows a workaround? Thanks
source
share