angular - 如何在 Angular/ ionic 的每个服务调用中显示/隐藏加载程序?

标签 angular ionic3 provider

我有一个提供程序,其中有很多函数,有没有办法在 ionic 中执行任何函数调用之前执行函数。 这是我的代码,我想在每次 HTTP 调用之前执行加载器函数 以简单的方式无需多次调用函数

 @Injectable()
  export class HttpProvider {
    BASEURL: string = 'https://www.ngrok.io';
    constructor(public http: HttpClient) {
      console.log('Hello HttpProvider Provider');
    }

    getCustomerInfo(customer_mobile) {
      return this.http.post(this.BASEURL + '/customer/get', {
        customer_mobile,
      });
    }

    addNewCustomer(customerInfo) {
      return this.http.post(this.BASEURL + '/customer/save', customerInfo);
    }
    updateCustomer(customerInfo) {
      return this.http.post(this.BASEURL + '/customer/update', customerInfo);
    }

    getVendorList(filterObject) {
      return this.http.post(this.BASEURL + '/getvendorlist', filterObject);
    }

    getVendorInfo(vendor_id) {
      return this.http.post(this.BASEURL + '/getvendorinfo', {
        vendor_id,
      });
    }

    updateAddress(updatedAddress) {
      return this.http.post(
        this.BASEURL + '/customeraddress/update',
        updatedAddress
      );
    }
    addAddress(newAddress) {
      return this.http.post(this.BASEURL + '/customeraddress/save', newAddress);
    }

    addOrder(orderDetails) {
      return this.http.post(this.BASEURL + '/order/save', orderDetails);
    }

    deleteAddress(address_id) {
      return this.http.post(this.BASEURL + '/customeraddress/delete', {
        address_id,
      });
    }
    getOrderList(customer_id, date?) {
      return this.http.post(
        this.BASEURL + '/fetchallcustomerorders',
        requestBody
      );
    }
    addReview(review: { feedback: string; rating: string; order_id: string }) {
      return this.http.post(this.BASEURL + '/orderreview', review);
    }


  }

最佳答案

您可以使用HTTPInterceptors在每次服务调用时显示/隐藏加载程序。

本教程将帮助您实现这一目标:Show Loader on every service call in angular

您可以在 intercept 方法中显示加载程序,一旦响应到来,您就可以隐藏它。

@Injectable()
export class MyInterceptor implements HttpInterceptor {
constructor(private loaderService: LoaderService) { }

intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {

    this.loaderService.show();

    return next
        .handle(req)
        .do(event => {
            if (event instanceof HttpResponse) {

                this.loaderService.hide();
            }
        });
  }

关于angular - 如何在 Angular/ ionic 的每个服务调用中显示/隐藏加载程序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54901968/

相关文章:

javascript - Angular 使用 Substring 和 ngIf 查看字符串是否在另一个字符串中

angular - 有没有办法显示遗漏的覆盖范围在 karma 覆盖范围内

Angular CDK 虚拟滚动无限循环

mobile - 通过手机号码自动确定SMS运营商提供的电子邮件

virtualbox - Vagrant VirtualBox 第二个磁盘路径

angular - Angular 7 中的中间件?

javascript - 如何使用 Ionic 3 记录并保存应用程序中的每次点击操作?

angular - Ionic 2 中的页面过渡动画

node.js - Electron js : use npm internally/programmatically

asp.net-mvc - 面向初学者的 ASP.NET MVC 自定义成员资格