如何在方法中显示 presentAlert,然后在 Ionic 4 上



我试图在"then方法"中显示一个presentAlert,在集合中添加一个文档(Firebase + Ionic 4,如果成功,它应该会出现警报(。但问题是,如果我从 then 方法内部调用,我无法访问 presentAlert 方法。

  addObject() {
this.afs.collection("Objects").add(this.object)
  .then(function () {
    console.log("Object successfully written!");
    this.presentAlert() //Doesn't work
  })
  .catch(function (error) {
    console.error(error);
  }); 
 }
  async presentAlert() {
    const alert = await this.alertController.create({
      header: 'Thank you!',
      message: 'This Object has been uploaded succesfully :)',
      buttons: ['OK']
    });
    await alert.present();
  }

更改以下内容:

  .then(function () {
    console.log("Object successfully written!");
    this.presentAlert() //Doesn't work
  })
  .catch(function (error) {
    console.error(error);
  }); 
 }

进入这个:

.then(() => {
    console.log("Object successfully written!");
    this.presentAlert() //Doesn't work
  })
  .catch((error) => {
    console.error(error);
  }); 
 }

相关内容

最新更新