如果这是在其他地方,我深表歉意,但我的常规搜索策略失败了。
当我从 API 收到 500 时,我想显示自定义错误页面。我正在使用 restangular 并有一个响应拦截器,当我收到 500 时重定向到错误页面,使用:
this.router.navigate(['/error']);
这一切都有效。但是,我想在浏览器导航栏中保留以前的 URL,这样当用户刷新时它会再次尝试以前的页面,而现在他们必须重新导航到损坏的页面才能再次尝试。
谢谢!
最佳答案
你可以添加这个:
setRouteErrorHandler(): void {
let errorRoute = null;
this.router.errorHandler = (error): void => {
this.location.go(errorRoute.url);
this.router.navigate(['/error'], { skipLocationChange: true, replaceUrl: true });
};
this.router.events
.filter((next) => next instanceOf NavigationError)
.subscribe((next) => {
errorRoute = next;
});
}
并从 AppModule 构造函数调用 setRouteErrorHandler()
或实现您自己的 APP_INITIALIZER
关于Angular2如何在保持路线的同时显示错误页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44075354/