I would make some workaround like:
let hasRouterError = false;
@Injectable()
export class MyErrorHandler implements ErrorHandler {
constructor(private inj: Injector) {}
handleError(error: any): void {
console.log('MyErrorHandler: ' + error);
if(hasRouterError) {
let router = this.inj.get(Router);
router.navigated = false;
}
}
}
export function MyRouterErrorHandler(error: any) {
console.log('RouterErrorHandler: ' + error);
hasRouterError = true;
throw error;
}
and we should use custom RouteReuseStrategy
:
export class PreventErrorRouteReuseStrategy implements RouteReuseStrategy {
shouldDetach(route: ActivatedRouteSnapshot): boolean { return false; }
store(route: ActivatedRouteSnapshot, detachedTree: DetachedRouteHandle): void {}
shouldAttach(route: ActivatedRouteSnapshot): boolean { return false; }
retrieve(route: ActivatedRouteSnapshot): DetachedRouteHandle|null { return null; }
shouldReuseRoute(future: ActivatedRouteSnapshot, curr: ActivatedRouteSnapshot): boolean {
if(hasRouterError) {
hasRouterError = false;
return false;
}
return future.routeConfig === curr.routeConfig;
}
}
It differs from DefaultRouteReuseStrategy
just this one code.
if(hasRouterError) {
hasRouterError = false;
return false;
}
don't forget to add it to the providers array:
import { RouteReuseStrategy } from '@angular/router';
...
{ provide: RouteReuseStrategy, useClass: PreventErrorRouteReuseStrategy },
You can try it in Modified Plunker
source
share