如何以 Angular2 方式处理 ajaxStart 和 ajaxStop 事件

How to handle ajaxStart and ajaxStop events in Angular2 way

本文关键字:ajaxStart ajaxStop 事件 处理 方式 Angular2      更新时间:2023-09-26

我需要触发类似于 JQuery 的事件 ajaxStart 和 ajaxStop我在这里找到了一半的解决方案->如何在 Angular2 中设置默认的 HTTP 标头?所以,目前我可以处理一个ajaxStart事件。

有什么解决方案如何处理ajaxStop事件吗?

post(url: string, body: string, options?: RequestOptionsArgs): Observable<Response> {
    //onAjaxStart
    $( "#loader" ).show();
    return super.post(url, body, options); //Any method here to handle event?
}

我尝试使用.catch(),但它仅在错误时触发

我需要在 ajax 触发时调用我的动画加载器,并在每次使用 ajax 请求时停止时隐藏它。

所以我正在尝试覆盖默认的 ajax 方法。但是我找不到正确的可观察方法(

你可以利用 "final" 运算符 observable。

以下是使用示例:

post(url: string, body: string, options?: RequestOptionsArgs): Observable<Response> {
    //onAjaxStart
    $( "#loader" ).show();
    return super.post(url, body, options).finally(() => {
      //onAjaxStop
      $( "#loader" ).hide();
    });
}