How to redirect correctly in a global error handler How to redirect correctly in a global error handler angular angular

How to redirect correctly in a global error handler


I've made some research and here is the reason why it behaves this way:

Since you're throwing an error in your service, the component which is using the service cannot be destroyed, thus the router is not able to release it. Thanks @TetraDev for pointing this out.

While someone might think it's an Angular bug I cannot confirm because I haven't found an open issue regarding it (feel free to update the answer if you have the link).

In order to resolve your problem I suggest not to use Router in this case and perform a browser redirect window.location.href = '/error'; instead.


It depends in where you have placed the router-outlet. if you something like this:

<router-outlet></router-outlet><!-- Home Component Content -->

It will loads just like you say, with the other components content. To avoid this behavior you have to struct your main component like this:

<router-outlet></router-outle>

Just that and the things you want in all pages, including the error one.