我會做一些變通方法,如:
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;
}
//throw error; // it is necessarily otherwise handleError won't be executed during next error
}
}
export function MyRouterErrorHandler(error: any) {
console.log('RouterErrorHandler: ' + error);
hasRouterError = true;
throw error;
}
,我們必須定製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;
}
}
它從DefaultRouteReuseStrategy
只有一個不同,該代碼
if(hasRouterError) {
hasRouterError = false;
return false;
}
不要忘記將其添加到提供商arr AY:
import { RouteReuseStrategy } from '@angular/router';
...
{ provide: RouteReuseStrategy, useClass: PreventErrorRouteReuseStrategy },
,您可以嘗試在Modified Plunker
檢查這個[問題](https://github.com/angular/angular/issues/2413)。可能會在下一個版本中修復 – PierreDuc
@PierreDuc我認爲這沒有幫助。您可以在plunker – yurzui
@PierreDuc設置4.1.1版本:yurzui是正確的,即使4.1.1問題仍然存在 –