从angular interceptor打开自定义组件

问题描述 投票:0回答:1

当我从后台收到特定错误时,是否可以在angular中从拦截器中打开自定义组件(带表单的对话窗口)?我正在努力寻找解决方法,但不能。

angular components interceptor
1个回答
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);
             }
           })
         )
     }

}
© www.soinside.com 2019 - 2024. All rights reserved.