Ionic4或3 LoadingController(进度对话框)

时间:2019-03-13 13:46:48

标签: ionic-framework ionic3 ionic4

我在应用程序中使用ionic4 LoadingController。在给定的时间间隔后将其关闭,但不要那样。我不想设定时间。在调用Web api之前,我将显示LoadingController,并且每当我从rest服务获得响应时都想关闭它。谁能帮我实现它?

3 个答案:

答案 0 :(得分:0)

您应该执行以下操作:

async function presentLoading() {
      const loader= document.querySelector('ion-loading-controller');
      await loader.componentOnReady();

      const element = await loader.create({
        message: 'Please wait...',
        spinner: 'crescent',
        duration: 2000
      });
      return await element .present();
}

答案 1 :(得分:0)

// Initialize variable
private loader: any;
private loaderActive: boolean = false;

// Generic method to display the loader
async showLoader() {
this.loaderActive = true;
this.loader = await this.loadingCtrl.create({
  message: 'Please wait...',
  spinner: 'crescent',
});
await this.loader.present();
}

// Generic method to dismiss the loader
async dismissLoader() {
if (this.loaderActive === true) {
  await this.loader.dismiss();
}
this.loaderActive = false;
}

Before api call just call the method this.showLoader(), once you get the response 
just call the this.dismissLoader() method.

答案 2 :(得分:0)

我会说您正在使用HttpClient从您的休息服务中获取数据,因此,我会为您提出这种情况, 首先,我们显示加载器:

let loader = await this.loadingCtrl.create({
      //Your loader content and options
    });
loader.present();//show the loader

此后,我们使用HttpClient获取数据。假设我们以public http: HttpClient的方式进行注入,因此它取决于您的其余服务http mthod(post,get,put ...)。该代码将是:

this.http.get("url").subscribe((data: any) => {
//process your data here
loader.dismiss();//then we hide the loader
});

如果我误解了,请留下评论。