Ionic?中警报按钮的可能作用

qyswt5oh  于 2022-12-08  发布在  Ionic
关注(0)|答案(2)|浏览(159)

在Ionic framework(v5)中,Alert上的按钮的role属性的可能值是什么?
文档仅提及cancel:https://ionicframework.com/docs/api/alert#buttons
您可以选择将role属性加入至按钮,例如cancel
示例:

buttons: [
        {
          text: 'Cancel',
          role: 'cancel',
          cssClass: 'secondary',
          handler: (blah) => {
            console.log('Confirm Cancel: blah');
          }
        }, {
          text: 'Okay',
          handler: () => {
            console.log('Confirm Okay');
          }
        }
      ]
uoifb46i

uoifb46i1#

I think role only have 1 property i.e 'cancel' .
As mentioned in Documentation:
"Optionally, a role property can be added to a button, such as cancel. If a cancel role is on one of the buttons, then if the alert is dismissed by tapping the backdrop, then it will fire the handler from the button with a cancel role."
So you can handle it when user dismiss it by clicking on backdrop. it means if you want to do something on cancellation but user dismiss alert by click outside(backdrop) you can still handle it.

7tofc5zh

7tofc5zh2#

Looking into the alert-interface.ts , the predefined roles are 'cancel' and 'destructive', but also it accepts any string you may want to use as roles.

export interface AlertButton {
  text: string;
  role?: 'cancel' | 'destructive' | string;
  cssClass?: string | string[];
  id?: string;
  handler?: (value: any) => boolean | void | {
    [key: string]: any;
  };
}

相关问题