Angular2如何在保持路线的同时显示错误页面 [英] Angular2 How to display error page while keeping route

查看:91
本文介绍了Angular2如何在保持路线的同时显示错误页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

很抱歉,如果在其他地方,但是我的常规搜索策略使我失败了.

Apologies if this is somewhere else, but my normal searching tactics have failed me.

当我从API收到500时,我想显示一个自定义错误页面.我正在使用restangular,并且有一个响应拦截器,当我收到500时,它会使用以下命令重定向到错误页面:

I'd like to display a custom error page when I receive a 500 from the API. I'm using restangular and have a response interceptor that redirects to the error page when I receive the 500, using:

this.router.navigate(['/error']);

这一切有效.但是,我想将先前的URL保留在浏览器导航栏中,以便在用户刷新时再次尝试上一页,而现在,他们必须重新导航到断开的页面才能再次尝试.

This all works. However, I'd like to keep the previous URL in the browser navigation bar so that when the user refreshes it tries the previous page again, whereas right now they have to re-navigate to the broken page to try again.

谢谢!

推荐答案

您可以添加以下内容:

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

and call setRouteErrorHandler() from AppModule constructor or implement your own APP_INITIALIZER

这篇关于Angular2如何在保持路线的同时显示错误页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆