Ionic 4 Loadingcontroller 覆盖不存在

Uma*_*han 5 ionic-framework ionic4

我创建了一个简单的函数来创建这样的加载

async presentLoading() {
  const loading = await this.loadingController.create({
    message: 'Please Wait...',
  });
  await loading.present();
}
Run Code Online (Sandbox Code Playgroud)

当像这样获取数据时,我正在关闭加载程序

  getUserData(){
  console.log(this.userID);
    this.api.getCompanyEmploye(this.userID).subscribe(res => {
     this.loadingController.dismiss(); //closing here 

      console.log(res);
      this.user = res.records[0];
      this.familyMembers = res.records[0].family_members;
    });
  }
Run Code Online (Sandbox Code Playgroud)

我在构造函数中调用这两个函数

constructor(public loadingController: LoadingController){
  this.presentLoading();
  this.getUserData();
}
Run Code Online (Sandbox Code Playgroud)

它的显示错误 ERROR Error: Uncaught (in promise): overlay does not exist

Ser*_*nko 10

问题是您的 API 调用响应比加载控制器实例化更快。您应该尝试以这种方式序列化它们,而不是并行调用:

使您的 presentLoading 方法返回 Promise:

async presentLoading() {
  const loading = await this.loadingController.create({
    message: 'Please Wait...',
  });
  return loading.present();
}
Run Code Online (Sandbox Code Playgroud)

现在你可以这样称呼它:

getUserData(){
  this.presentLoading().then(()=>{
     this.api.getCompanyEmploye(this.userID).subscribe(res => {
        this.loadingController.dismiss(); //closing here 
        console.log(res);
        this.user = res.records[0];
        this.familyMembers = res.records[0].family_members;
    });
  })
}
Run Code Online (Sandbox Code Playgroud)

在您的构造函数中,您只需要调用 API