离子 4 加载控制器

Ionic 4 LoadingController

我正在尝试将 LoadingController 添加到我的 Ionic 5 应用程序。

使用以下代码,加载微调器出现:

async presentLoading() {
    const loading = await this.loadingCtrl.create({
      message: 'Please wait...',
    });
    await loading.present();
  }

getPosts() {

    this.posts = [];
    this.presentLoading();

    query.get()
      .then((docs) => {
        docs.forEach((doc) => {
          this.posts.push(doc);
        })
      }).catch((err) => {
        console.log(err)
      })

}

但我不知道如何在填充 posts 数组后关闭 LoadingController。

有人可以告诉我这是怎么做到的吗?

你必须 dismiss 控制器。为此,您必须保留对它的引用,像这样,

async presentLoading() {
    this.loading = await this.loadingCtrl.create({
        message: 'Please wait...',
    });
    await this.loading.present();
}
getPosts() {
    this.posts = [];
    this.presentLoading();
    query.get()
    .then((docs) => {
        docs.forEach((doc) => {
            this.posts.push(doc);
            this.loading.dismiss();
        })
    }).catch((err) => {
        console.log(err)
    })
}

如果需要在dismiss时得到通知,可以监听onDidDismiss事件。

链接: