如何在错误重定向时关闭打开的弹出窗口?
How to close opened popups on error redirection?
在拦截器中,我有在发生错误时重定向页面的机制。问题是,如果已经打开了弹出窗口,它们将不会关闭。他们后面会出现错误页面。
这是重定向代码:
return next.handle(request).pipe(
(error: any) => {
if (error.status === BAD_REQUEST) {
}else{
this.router.navigate(['/error/']);
}
});
关闭弹出窗口的一种方法是使用具有将发出值的主题的服务。
对于这种情况,您可以订阅该主题并在收到每个值时关闭这些弹出窗口;
// http-errors.service.ts
private _errorReceived$ = new Subject();
errorReceived$ = this._errorReceived$.asObservable();
handleError () {
// We're not particularly interested in the error message
this._errorReceived$.next();
}
// component-which-holds-popups.component.ts
this.httpErrorsService.errorReceived$
.subscribe(() => {
this.shouldHidePopups = true;
});
// interceptor
return next.handle(req)
.pipe(
catchError(err => {
if (err instanceof HttpErrorResponse && err.status === YOUR_ERR_STATUS) {
this.httpErrorsService.handleError();
this.redirectToErrPage();
}
return throwError(err);
})
)
<!-- component-which-holds-popups.component.html -->
<!-- ... -->
<app-popup *ngIf="!shouldHidePopups"></app-popup>
<!-- ... -->
在拦截器中,我有在发生错误时重定向页面的机制。问题是,如果已经打开了弹出窗口,它们将不会关闭。他们后面会出现错误页面。
这是重定向代码:
return next.handle(request).pipe(
(error: any) => {
if (error.status === BAD_REQUEST) {
}else{
this.router.navigate(['/error/']);
}
});
关闭弹出窗口的一种方法是使用具有将发出值的主题的服务。 对于这种情况,您可以订阅该主题并在收到每个值时关闭这些弹出窗口;
// http-errors.service.ts
private _errorReceived$ = new Subject();
errorReceived$ = this._errorReceived$.asObservable();
handleError () {
// We're not particularly interested in the error message
this._errorReceived$.next();
}
// component-which-holds-popups.component.ts
this.httpErrorsService.errorReceived$
.subscribe(() => {
this.shouldHidePopups = true;
});
// interceptor
return next.handle(req)
.pipe(
catchError(err => {
if (err instanceof HttpErrorResponse && err.status === YOUR_ERR_STATUS) {
this.httpErrorsService.handleError();
this.redirectToErrPage();
}
return throwError(err);
})
)
<!-- component-which-holds-popups.component.html -->
<!-- ... -->
<app-popup *ngIf="!shouldHidePopups"></app-popup>
<!-- ... -->