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

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

问题描述

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

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

当我从 API 收到 500 时,我想显示一个自定义错误页面.我正在使用 retangular 并有一个响应拦截器,当我收到 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天全站免登陆