在 Ionic 上单击警告框外部时如何不关闭警告框

How to not dismiss the alert box when clicking outside of it on Ionic

我正在构建一个 ionic 2 应用程序,我正在使用以下组件

http://ionicframework.com/docs/components/#alert

  import { AlertController } from 'ionic-angular';

export class MyPage {
  constructor(public alertCtrl: AlertController) {
  }

  showAlert() {
    let alert = this.alertCtrl.create({
      title: 'New Friend!',
      subTitle: 'Your friend, Obi wan Kenobi, just accepted your friend request!',
      buttons: ['OK']
    });
    alert.present();
  }
}

如何确保当我在框外单击时不会关闭警报?

在 alertCtrl.create 选项中设置 enableBackdropDismiss: false

离子 2/3:

如您在 the AlertController docs 中所见,您可以在创建警报时使用 enableBackdropDismiss(布尔值)选项:

enableBackdropDismiss: Whether the alert should be dismissed by tapping the backdrop. Default true

import { AlertController } from 'ionic-angular';

// ...
export class MyPage {

  constructor(public alertCtrl: AlertController) {}

  showAlert() {
    let alert = this.alertCtrl.create({
      title: 'New Friend!',
      subTitle: 'Your friend, Obi wan Kenobi, just accepted your friend request!',
      buttons: ['OK'],
      enableBackdropDismiss: false // <- Here! :)
    });

    alert.present();
  }
}

离子 4/5:

在 Ionic 4/5 中 this property 已重命名为 backdropDismiss:

backdropDismiss: If true, the alert will be dismissed when the backdrop is clicked.

import { AlertController } from '@ionic/angular';

//...
export class MyPage {

  constructor(public alertController: AlertController) {}

  async showAlert() {
    const alert = await this.alertController.create({
      header: 'Alert',
      subHeader: 'Subtitle',
      message: 'This is an alert message.',
      buttons: ['OK'],
      backdropDismiss: false // <- Here! :)
    });

    await alert.present();
  }
}

在 ionic 4 中,该选项已重命名为

backdropDismiss: false