从角度拦截器打开自定义组件

时间:2020-05-27 18:16:54

标签: angular components interceptor

当我从后端收到特定错误时,是否可以从拦截器打开自定义组件(带有窗体的对话框)?我正试图为此找到解决方案的可行解决方案。

1 个答案:

答案 0 :(得分:0)

假设您的模态组件名称为MyModalComponent,而您想在其上打开它,则拦截器将类似于

    export class HttpErrorInterceptor implements HttpInterceptor {
     constructor(modalService: BsModalService) {}
     intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
       return next.handle(request)
         .pipe(
           catchError((error: HttpErrorResponse) => {
             if(error.status === '404') {
             this.modalService.show(MyModalComponent, {});
             return throwError(errorMessage);
             }
           })
         )
     }

}