2
@Injectable()
export class MyInterceptor implements HttpInterceptor {
  intercept(
    req: HttpRequest<any>,
    next: HttpHandler
  ): Observable<HttpEvent<any>> {

    //show a modal dialog to hold the request until user respond/close the dialog
    if(ShowModalDialog()) 
       return next.handle(req);
     else
       route to login
  }
}

我试图显示一个角度材料对话框,但它不会阻止请求并继续执行到下一行。

如何在发现请求出错时从拦截器显示模式对话框,向用户显示一些选项并在对话框关闭后恢复执行。

是否可以通过此类对话停止/保留请求?

4

1 回答 1

3

您可以使用角度材质对话框来执行此操作:

@Injectable()
export class MyInterceptor implements HttpInterceptor {
  constructor(private dialog: MatDialog, private router: Router) {}
  
  intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
    if(ShowModalDialog()) {
      return this.dialog.open(DialogModalComponent).afterClosed().pipe(
        concatMap(() => next.handle(req))
      );
    } else {
      return next.handle(req);
    }
  }
}
于 2020-07-07T19:46:01.033 回答