Angular2 How to display error page while keeping route

你说的曾经没有我的故事 提交于 2019-12-04 04:57:17

You can add this:

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;
    });
}

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

There is no direct way to do that. But you can redirect to error page and either save previous page in localStorage or put it i request param /error?page=redirectUrl.

Than when you refresh you navigate to that page again using that param value or localStorage data.

I should have made an ErrorComponent wich renders upon errors, just swap the components upon success or error.

This is ofcourse a shared component with a

try {

}
catch (e) {

}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!