Ion-alert
là một hộp thoại hiển thị cho người dùng thông tin hoặc thu thập thông tin từ người dùng bằng cách sử dụng đầu vào. Một cảnh báo xuất hiện trên đầu nội dung của ứng dụng và người dùng phải loại bỏ theo cách thủ công trước khi họ có thể tiếp tục tương tác với ứng dụng. Nó cũng có thể tùy chọn có một header, subHeadervà message.
Button
Trong mảng buttons, mỗi nút bao gồm các thuộc tính cho nó textvà tùy chọn a handler. Nếu một trình xử lý quay trở lại falsethì cảnh báo sẽ không tự động bị loại bỏ khi nút được nhấp. Tất cả các nút sẽ hiển thị theo thứ tự chúng đã được thêm vào buttonsmảng từ trái sang phải. Lưu ý: Nút ngoài cùng bên phải (nút cuối cùng trong mảng) là nút chính.
Theo tùy chọn, một thuộc roletính có thể được thêm vào một nút, chẳng hạn như cancel. Nếu một cancelvai trò nằm trên một trong các nút, thì nếu cảnh báo bị loại bỏ bằng cách nhấn vào phông nền, thì nó sẽ kích hoạt trình xử lý khỏi nút với vai trò hủy.
inputs
Ion-alert cũng có thể bao gồm một số đầu vào khác nhau mà dữ liệu của chúng có thể được chuyển trở lại ứng dụng. Đầu vào có thể được sử dụng như một cách đơn giản để nhắc người dùng về thông tin. Bộ đàm, hộp kiểm và đầu vào văn bản đều được chấp nhận, nhưng chúng không thể bị trộn lẫn. Ví dụ: một cảnh báo có thể có tất cả đầu vào nút radio hoặc tất cả đầu vào hộp kiểm, nhưng cùng một cảnh báo không thể kết hợp đầu vào radio và hộp kiểm. Do lưu ý tuy nhiên, loại khác nhau của nguyên liệu đầu vào "văn bản" có thể được trộn lẫn, chẳng hạn như url, email, text, textareavv Nếu bạn yêu cầu một giao diện người dùng dạng phức tạp mà không phù hợp trong các nguyên tắc của một cảnh báo sau đó chúng tôi khuyên bạn nên thiết kế Form trong một phương thức thay vì .
Customization
Alert sử dụng tính năng đóng gói theo phạm vi, có nghĩa là nó sẽ tự động mở rộng phạm vi CSS của mình bằng cách nối mỗi kiểu với một lớp bổ sung trong thời gian chạy. Ghi đè các bộ chọn theo phạm vi trong CSS yêu cầu một bộ chọn có Higher specificity selector.
Chúng tôi khuyên bạn nên chuyển một lớp tùy chỉnh vào cssClasstrong createphương thức và sử dụng lớp đó để thêm các kiểu tùy chỉnh vào máy chủ và các phần tử bên trong. Thuộc tính này cũng có thể chấp nhận nhiều lớp được phân tách bằng dấu cách. Xem sử dụng cho một ví dụ về cách vượt qua một lớp bằng cách sử dụng cssClass.
Bất kỳ trong số những điều đã xác định Thuộc tính tùy chỉnh CSS có thể được sử dụng để tạo kiểu cho Cảnh báo mà không cần nhắm mục tiêu các phần tử riêng lẻ:
Usage
import { Component } from '@angular/core';
import { AlertController } from '@ionic/angular';
@Component({
selector: 'alert-example',
templateUrl: 'alert-example.html',
styleUrls: ['./alert-example.css'],
})
export class AlertExample {
constructor(public alertController: AlertController) {}
async presentAlert() {
const alert = await this.alertController.create({
cssClass: 'my-custom-class',
header: 'Alert',
subHeader: 'Subtitle',
message: 'This is an alert message.',
buttons: ['OK']
});
await alert.present();
const { role } = await alert.onDidDismiss();
console.log('onDidDismiss resolved with role', role);
}
async presentAlertMultipleButtons() {
const alert = await this.alertController.create({
cssClass: 'my-custom-class',
header: 'Alert',
subHeader: 'Subtitle',
message: 'This is an alert message.',
buttons: ['Cancel', 'Open Modal', 'Delete']
});
await alert.present();
}
async presentAlertConfirm() {
const alert = await this.alertController.create({
cssClass: 'my-custom-class',
header: 'Confirm!',
message: 'Message <strong>text</strong>!!!',
buttons: [
{
text: 'Cancel',
role: 'cancel',
cssClass: 'secondary',
handler: (blah) => {
console.log('Confirm Cancel: blah');
}
}, {
text: 'Okay',
handler: () => {
console.log('Confirm Okay');
}
}
]
});
await alert.present();
}
async presentAlertPrompt() {
const alert = await this.alertController.create({
cssClass: 'my-custom-class',
header: 'Prompt!',
inputs: [
{
name: 'name1',
type: 'text',
placeholder: 'Placeholder 1'
},
{
name: 'name2',
type: 'text',
id: 'name2-id',
value: 'hello',
placeholder: 'Placeholder 2'
},
// multiline input.
{
name: 'paragraph',
id: 'paragraph',
type: 'textarea',
placeholder: 'Placeholder 3'
},
{
name: 'name3',
value: 'http://ionicframework.com',
type: 'url',
placeholder: 'Favorite site ever'
},
// input date with min & max
{
name: 'name4',
type: 'date',
min: '2017-03-01',
max: '2018-01-12'
},
// input date without min nor max
{
name: 'name5',
type: 'date'
},
{
name: 'name6',
type: 'number',
min: -5,
max: 10
},
{
name: 'name7',
type: 'number'
},
{
name: 'name8',
type: 'password',
placeholder: 'Advanced Attributes',
cssClass: 'specialClass',
attributes: {
maxlength: 4,
inputmode: 'decimal'
}
}
],
buttons: [
{
text: 'Cancel',
role: 'cancel',
cssClass: 'secondary',
handler: () => {
console.log('Confirm Cancel');
}
}, {
text: 'Ok',
handler: () => {
console.log('Confirm Ok');
}
}
]
});
await alert.present();
}
async presentAlertRadio() {
const alert = await this.alertController.create({
cssClass: 'my-custom-class',
header: 'Radio',
inputs: [
{
name: 'radio1',
type: 'radio',
label: 'Radio 1',
value: 'value1',
handler: () => {
console.log('Radio 1 selected');
},
checked: true
},
{
name: 'radio2',
type: 'radio',
label: 'Radio 2',
value: 'value2',
handler: () => {
console.log('Radio 2 selected');
}
},
{
name: 'radio3',
type: 'radio',
label: 'Radio 3',
value: 'value3',
handler: () => {
console.log('Radio 3 selected');
}
},
{
name: 'radio4',
type: 'radio',
label: 'Radio 4',
value: 'value4',
handler: () => {
console.log('Radio 4 selected');
}
},
{
name: 'radio5',
type: 'radio',
label: 'Radio 5',
value: 'value5',
handler: () => {
console.log('Radio 5 selected');
}
},
{
name: 'radio6',
type: 'radio',
label: 'Radio 6 Radio 6 Radio 6 Radio 6 Radio 6 Radio 6 Radio 6 Radio 6 Radio 6 Radio 6 ',
value: 'value6',
handler: () => {
console.log('Radio 6 selected');
}
}
],
buttons: [
{
text: 'Cancel',
role: 'cancel',
cssClass: 'secondary',
handler: () => {
console.log('Confirm Cancel');
}
}, {
text: 'Ok',
handler: () => {
console.log('Confirm Ok');
}
}
]
});
await alert.present();
}
async presentAlertCheckbox() {
const alert = await this.alertController.create({
cssClass: 'my-custom-class',
header: 'Checkbox',
inputs: [
{
name: 'checkbox1',
type: 'checkbox',
label: 'Checkbox 1',
value: 'value1',
handler: () => {
console.log('Checkbox 1 selected');
},
checked: true
},
{
name: 'checkbox2',
type: 'checkbox',
label: 'Checkbox 2',
value: 'value2',
handler: () => {
console.log('Checkbox 2 selected');
}
},
{
name: 'checkbox3',
type: 'checkbox',
label: 'Checkbox 3',
value: 'value3',
handler: () => {
console.log('Checkbox 3 selected');
}
},
{
name: 'checkbox4',
type: 'checkbox',
label: 'Checkbox 4',
value: 'value4',
handler: () => {
console.log('Checkbox 4 selected');
}
},
{
name: 'checkbox5',
type: 'checkbox',
label: 'Checkbox 5',
value: 'value5',
handler: () => {
console.log('Checkbox 5 selected');
}
},
{
name: 'checkbox6',
type: 'checkbox',
label: 'Checkbox 6 Checkbox 6 Checkbox 6 Checkbox 6 Checkbox 6 Checkbox 6 Checkbox 6 Checkbox 6 Checkbox 6 Checkbox 6',
value: 'value6',
handler: () => {
console.log('Checkbox 6 selected');
}
}
],
buttons: [
{
text: 'Cancel',
role: 'cancel',
cssClass: 'secondary',
handler: () => {
console.log('Confirm Cancel');
}
}, {
text: 'Ok',
handler: () => {
console.log('Confirm Ok');
}
}
]
});
await alert.present();
}
}function presentAlert() {
const alert = document.createElement('ion-alert');
alert.cssClass = 'my-custom-class';
alert.header = 'Alert';
alert.subHeader = 'Subtitle';
alert.message = 'This is an alert message.';
alert.buttons = ['OK'];
document.body.appendChild(alert);
await alert.present();
const { role } = await alert.onDidDismiss();
console.log('onDidDismiss resolved with role', role);
}
function presentAlertMultipleButtons() {
const alert = document.createElement('ion-alert');
alert.cssClass = 'my-custom-class';
alert.header = 'Alert';
alert.subHeader = 'Subtitle';
alert.message = 'This is an alert message.';
alert.buttons = ['Cancel', 'Open Modal', 'Delete'];
document.body.appendChild(alert);
return alert.present();
}
function presentAlertConfirm() {
const alert = document.createElement('ion-alert');
alert.cssClass = 'my-custom-class';
alert.header = 'Confirm!';
alert.message = 'Message <strong>text</strong>!!!';
alert.buttons = [
{
text: 'Cancel',
role: 'cancel',
cssClass: 'secondary',
handler: (blah) => {
console.log('Confirm Cancel: blah');
}
}, {
text: 'Okay',
handler: () => {
console.log('Confirm Okay')
}
}
];
document.body.appendChild(alert);
return alert.present();
}
function presentAlertPrompt() {
const alert = document.createElement('ion-alert');
alert.cssClass = 'my-custom-class';
alert.header = 'Prompt!';
alert.inputs = [
{
placeholder: 'Placeholder 1'
},
{
name: 'name2',
id: 'name2-id',
value: 'hello',
placeholder: 'Placeholder 2'
},
// multiline input.
{
name: 'paragraph',
id: 'paragraph',
type: 'textarea',
placeholder: 'Placeholder 3'
},
{
name: 'name3',
value: 'http://ionicframework.com',
type: 'url',
placeholder: 'Favorite site ever'
},
// input date with min & max
{
name: 'name4',
type: 'date',
min: '2017-03-01',
max: '2018-01-12'
},
// input date without min nor max
{
name: 'name5',
type: 'date'
},
{
name: 'name6',
type: 'number',
min: -5,
max: 10
},
{
name: 'name7',
type: 'number'
},
{
name: 'name8',
type: 'password',
placeholder: 'Advanced Attributes',
cssClass: 'specialClass',
attributes: {
maxlength: 4,
inputmode: 'decimal'
}
}
];
alert.buttons = [
{
text: 'Cancel',
role: 'cancel',
cssClass: 'secondary',
handler: () => {
console.log('Confirm Cancel')
}
}, {
text: 'Ok',
handler: () => {
console.log('Confirm Ok')
}
}
];
document.body.appendChild(alert);
return alert.present();
}
function presentAlertRadio() {
const alert = document.createElement('ion-alert');
alert.cssClass = 'my-custom-class';
alert.header = 'Radio';
alert.inputs = [
{
type: 'radio',
label: 'Radio 1',
value: 'value1',
handler: () => {
console.log('Radio 1 selected');
},
checked: true
},
{
type: 'radio',
label: 'Radio 2',
value: 'value2',
handler: () => {
console.log('Radio 2 selected');
}
},
{
type: 'radio',
label: 'Radio 3',
value: 'value3',
handler: () => {
console.log('Radio 3 selected');
}
},
{
type: 'radio',
label: 'Radio 4',
value: 'value4',
handler: () => {
console.log('Radio 4 selected');
}
},
{
type: 'radio',
label: 'Radio 5',
value: 'value5',
handler: () => {
console.log('Radio 5 selected');
}
},
{
type: 'radio',
label: 'Radio 6 Radio 6 Radio 6 Radio 6 Radio 6 Radio 6 Radio 6 Radio 6 Radio 6 Radio 6 ',
value: 'value6',
handler: () => {
console.log('Radio 6 selected');
}
}
];
alert.buttons = [
{
text: 'Cancel',
role: 'cancel',
cssClass: 'secondary',
handler: () => {
console.log('Confirm Cancel')
}
}, {
text: 'Ok',
handler: () => {
console.log('Confirm Ok')
}
}
];
document.body.appendChild(alert);
return alert.present();
}
function presentAlertCheckbox() {
const alert = document.createElement('ion-alert');
alert.cssClass = 'my-custom-class';
alert.header = 'Checkbox';
alert.inputs = [
{
type: 'checkbox',
label: 'Checkbox 1',
value: 'value1',
handler: () => {
console.log('Checkbox 1 selected');
},
checked: true
},
{
type: 'checkbox',
label: 'Checkbox 2',
value: 'value2',
handler: () => {
console.log('Checkbox 2 selected');
}
},
{
type: 'checkbox',
label: 'Checkbox 3',
value: 'value3',
handler: () => {
console.log('Checkbox 3 selected');
}
},
{
type: 'checkbox',
label: 'Checkbox 4',
value: 'value4',
handler: () => {
console.log('Checkbox 4 selected');
}
},
{
type: 'checkbox',
label: 'Checkbox 5',
value: 'value5',
handler: () => {
console.log('Checkbox 5 selected');
}
},
{
type: 'checkbox',
label: 'Checkbox 6 Checkbox 6 Checkbox 6 Checkbox 6 Checkbox 6 Checkbox 6 Checkbox 6 Checkbox 6 Checkbox 6 Checkbox 6',
value: 'value6',
handler: () => {
console.log('Checkbox 6 selected');
}
}
];
alert.buttons = [
{
text: 'Cancel',
role: 'cancel',
cssClass: 'secondary',
handler: () => {
console.log('Confirm Cancel')
}
}, {
text: 'Ok',
handler: () => {
console.log('Confirm Ok')
}
}
];
document.body.appendChild(alert);
return alert.present();
}
/* Using with useIonAlert Hook */
import React from 'react';
import { IonButton, IonContent, IonPage, useIonAlert } from '@ionic/react';
const AlertExample: React.FC = () => {
const [present] = useIonAlert();
return (
<IonPage>
<IonContent fullscreen>
<IonButton
expand="block"
onClick={() =>
present({
cssClass: 'my-css',
header: 'Alert',
message: 'alert from hook',
buttons: [
'Cancel',
{ text: 'Ok', handler: (d) => console.log('ok pressed') },
],
onDidDismiss: (e) => console.log('did dismiss'),
})
}
>
Show Alert
</IonButton>
<IonButton
expand="block"
onClick={() => present('hello with params', [{ text: 'Ok' }])}
>
Show Alert using params
</IonButton>
</IonContent>
</IonPage>
);
};
CopyCopied/* Using with IonAlert Component */
import React, { useState } from 'react';
import { IonAlert, IonButton, IonContent } from '@ionic/react';
export const AlertExample: React.FC = () => {
const [showAlert1, setShowAlert1] = useState(false);
const [showAlert2, setShowAlert2] = useState(false);
const [showAlert3, setShowAlert3] = useState(false);
const [showAlert4, setShowAlert4] = useState(false);
const [showAlert5, setShowAlert5] = useState(false);
const [showAlert6, setShowAlert6] = useState(false);
return (
<IonContent>
<IonButton onClick={() => setShowAlert1(true)} expand="block">Show Alert 1</IonButton>
<IonButton onClick={() => setShowAlert2(true)} expand="block">Show Alert 2</IonButton>
<IonButton onClick={() => setShowAlert3(true)} expand="block">Show Alert 3</IonButton>
<IonButton onClick={() => setShowAlert4(true)} expand="block">Show Alert 4</IonButton>
<IonButton onClick={() => setShowAlert5(true)} expand="block">Show Alert 5</IonButton>
<IonButton onClick={() => setShowAlert6(true)} expand="block">Show Alert 6</IonButton>
<IonAlert
isOpen={showAlert1}
onDidDismiss={() => setShowAlert1(false)}
cssClass='my-custom-class'
header={'Alert'}
subHeader={'Subtitle'}
message={'This is an alert message.'}
buttons={['OK']}
/>
<IonAlert
isOpen={showAlert2}
onDidDismiss={() => setShowAlert2(false)}
cssClass='my-custom-class'
header={'Alert'}
subHeader={'Subtitle'}
message={'This is an alert message.'}
buttons={['Cancel', 'Open Modal', 'Delete']}
/>
<IonAlert
isOpen={showAlert3}
onDidDismiss={() => setShowAlert3(false)}
cssClass='my-custom-class'
header={'Confirm!'}
message={'Message <strong>text</strong>!!!'}
buttons={[
{
text: 'Cancel',
role: 'cancel',
cssClass: 'secondary',
handler: blah => {
console.log('Confirm Cancel: blah');
}
},
{
text: 'Okay',
handler: () => {
console.log('Confirm Okay');
}
}
]}
/>
<IonAlert
isOpen={showAlert4}
onDidDismiss={() => setShowAlert4(false)}
cssClass='my-custom-class'
header={'Prompt!'}
inputs={[
{
name: 'name1',
type: 'text',
placeholder: 'Placeholder 1'
},
{
name: 'name2',
type: 'text',
id: 'name2-id',
value: 'hello',
placeholder: 'Placeholder 2'
},
{
name: 'name3',
value: 'http://ionicframework.com',
type: 'url',
placeholder: 'Favorite site ever'
},
// input date with min & max
{
name: 'name4',
type: 'date',
min: '2017-03-01',
max: '2018-01-12'
},
// input date without min nor max
{
name: 'name5',
type: 'date'
},
{
name: 'name6',
type: 'number',
min: -5,
max: 10
},
{
name: 'name7',
type: 'number'
},
{
name: 'name8',
type: 'password',
placeholder: 'Advanced Attributes',
cssClass: 'specialClass',
attributes: {
maxlength: 4,
inputmode: 'decimal'
}
}
]}
buttons={[
{
text: 'Cancel',
role: 'cancel',
cssClass: 'secondary',
handler: () => {
console.log('Confirm Cancel');
}
},
{
text: 'Ok',
handler: () => {
console.log('Confirm Ok');
}
}
]}
/>
<IonAlert
isOpen={showAlert5}
onDidDismiss={() => setShowAlert5(false)}
cssClass='my-custom-class'
header={'Radio'}
inputs={[
{
name: 'radio1',
type: 'radio',
label: 'Radio 1',
value: 'value1',
handler: () => {
console.log('Radio 1 selected');
},
checked: true
},
{
name: 'radio2',
type: 'radio',
label: 'Radio 2',
value: 'value2',
handler: () => {
console.log('Radio 2 selected');
}
},
{
name: 'radio3',
type: 'radio',
label: 'Radio 3',
value: 'value3',
handler: () => {
console.log('Radio 3 selected');
}
},
{
name: 'radio4',
type: 'radio',
label: 'Radio 4',
value: 'value4',
handler: () => {
console.log('Radio 4 selected');
}
},
{
name: 'radio5',
type: 'radio',
label: 'Radio 5',
value: 'value5',
handler: () => {
console.log('Radio 5 selected');
}
},
{
name: 'radio6',
type: 'radio',
label: 'Radio 6',
value: 'value6',
handler: () => {
console.log('Radio 6 selected');
}
}
]}
buttons={[
{
text: 'Cancel',
role: 'cancel',
cssClass: 'secondary',
handler: () => {
console.log('Confirm Cancel');
}
},
{
text: 'Ok',
handler: () => {
console.log('Confirm Ok');
}
}
]}
/>
<IonAlert
isOpen={showAlert6}
onDidDismiss={() => setShowAlert6(false)}
cssClass='my-custom-class'
header={'Checkbox'}
inputs={[
{
name: 'checkbox1',
type: 'checkbox',
label: 'Checkbox 1',
value: 'value1',
handler: () => {
console.log('Checkbox 1 selected');
},
checked: true
},
{
name: 'checkbox2',
type: 'checkbox',
label: 'Checkbox 2',
value: 'value2',
handler: () => {
console.log('Checkbox 2 selected');
}
},
{
name: 'checkbox3',
type: 'checkbox',
label: 'Checkbox 3',
value: 'value3',
handler: () => {
console.log('Checkbox 3 selected');
}
},
{
name: 'checkbox4',
type: 'checkbox',
label: 'Checkbox 4',
value: 'value4',
handler: () => {
console.log('Checkbox 4 selected');
}
},
{
name: 'checkbox5',
type: 'checkbox',
label: 'Checkbox 5',
value: 'value5',
handler: () => {
console.log('Checkbox 5 selected');
}
},
{
name: 'checkbox6',
type: 'checkbox',
label: 'Checkbox 6',
value: 'value6',
handler: () => {
console.log('Checkbox 6 selected');
}
}
]}
buttons={[
{
text: 'Cancel',
role: 'cancel',
cssClass: 'secondary',
handler: () => {
console.log('Confirm Cancel');
}
},
{
text: 'Ok',
handler: () => {
console.log('Confirm Ok');
}
}
]}
/>
</IonContent>
);
}
export default AlertExample;
import { Component, h } from '@stencil/core';
import { alertController } from '@ionic/core';
@Component({
tag: 'alert-example',
styleUrl: 'alert-example.css'
})
export class AlertExample {
async presentAlert() {
const alert = await alertController.create({
cssClass: 'my-custom-class',
header: 'Alert',
subHeader: 'Subtitle',
message: 'This is an alert message.',
buttons: ['OK']
});
await alert.present();
const { role } = await alert.onDidDismiss();
console.log('onDidDismiss resolved with role', role);
}
async presentAlertMultipleButtons() {
const alert = await alertController.create({
cssClass: 'my-custom-class',
header: 'Alert',
subHeader: 'Subtitle',
message: 'This is an alert message.',
buttons: ['Cancel', 'Open Modal', 'Delete']
});
await alert.present();
}
async presentAlertConfirm() {
const alert = await alertController.create({
cssClass: 'my-custom-class',
header: 'Confirm!',
message: 'Message <strong>text</strong>!!!',
buttons: [
{
text: 'Cancel',
role: 'cancel',
cssClass: 'secondary',
handler: (blah) => {
console.log('Confirm Cancel: blah');
}
}, {
text: 'Okay',
handler: () => {
console.log('Confirm Okay');
}
}
]
});
await alert.present();
}
async presentAlertPrompt() {
const alert = await alertController.create({
cssClass: 'my-custom-class',
header: 'Prompt!',
inputs: [
{
name: 'name1',
type: 'text',
placeholder: 'Placeholder 1'
},
{
name: 'name2',
type: 'text',
id: 'name2-id',
value: 'hello',
placeholder: 'Placeholder 2'
},
// multiline input.
{
name: 'paragraph',
id: 'paragraph',
type: 'textarea',
placeholder: 'Placeholder 3'
},
{
name: 'name3',
value: 'http://ionicframework.com',
type: 'url',
placeholder: 'Favorite site ever'
},
// input date with min & max
{
name: 'name4',
type: 'date',
min: '2017-03-01',
max: '2018-01-12'
},
// input date without min nor max
{
name: 'name5',
type: 'date'
},
{
name: 'name6',
type: 'number',
min: -5,
max: 10
},
{
name: 'name7',
type: 'number'
},
{
name: 'name8',
type: 'password',
placeholder: 'Advanced Attributes',
cssClass: 'specialClass',
attributes: {
maxlength: 4,
inputmode: 'decimal'
}
}
],
buttons: [
{
text: 'Cancel',
role: 'cancel',
cssClass: 'secondary',
handler: () => {
console.log('Confirm Cancel');
}
}, {
text: 'Ok',
handler: () => {
console.log('Confirm Ok');
}
}
]
});
await alert.present();
}
async presentAlertRadio() {
const alert = await alertController.create({
cssClass: 'my-custom-class',
header: 'Radio',
inputs: [
{
name: 'radio1',
type: 'radio',
label: 'Radio 1',
value: 'value1',
handler: () => {
console.log('Radio 1 selected');
},
checked: true
},
{
name: 'radio2',
type: 'radio',
label: 'Radio 2',
value: 'value2',
handler: () => {
console.log('Radio 2 selected');
}
},
{
name: 'radio3',
type: 'radio',
label: 'Radio 3',
value: 'value3',
handler: () => {
console.log('Radio 3 selected');
}
},
{
name: 'radio4',
type: 'radio',
label: 'Radio 4',
value: 'value4',
handler: () => {
console.log('Radio 4 selected');
}
},
{
name: 'radio5',
type: 'radio',
label: 'Radio 5',
value: 'value5',
handler: () => {
console.log('Radio 5 selected');
}
},
{
name: 'radio6',
type: 'radio',
label: 'Radio 6 Radio 6 Radio 6 Radio 6 Radio 6 Radio 6 Radio 6 Radio 6 Radio 6 Radio 6 ',
value: 'value6',
handler: () => {
console.log('Radio 6 selected');
}
}
],
buttons: [
{
text: 'Cancel',
role: 'cancel',
cssClass: 'secondary',
handler: () => {
console.log('Confirm Cancel');
}
}, {
text: 'Ok',
handler: () => {
console.log('Confirm Ok');
}
}
]
});
await alert.present();
}
async presentAlertCheckbox() {
const alert = await alertController.create({
cssClass: 'my-custom-class',
header: 'Checkbox',
inputs: [
{
name: 'checkbox1',
type: 'checkbox',
label: 'Checkbox 1',
value: 'value1',
handler: () => {
console.log('Checkbox 1 selected');
},
checked: true
},
{
name: 'checkbox2',
type: 'checkbox',
label: 'Checkbox 2',
value: 'value2',
handler: () => {
console.log('Checkbox 2 selected');
}
},
{
name: 'checkbox3',
type: 'checkbox',
label: 'Checkbox 3',
value: 'value3',
handler: () => {
console.log('Checkbox 3 selected');
}
},
{
name: 'checkbox4',
type: 'checkbox',
label: 'Checkbox 4',
value: 'value4',
handler: () => {
console.log('Checkbox 4 selected');
}
},
{
name: 'checkbox5',
type: 'checkbox',
label: 'Checkbox 5',
value: 'value5',
handler: () => {
console.log('Checkbox 5 selected');
}
},
{
name: 'checkbox6',
type: 'checkbox',
label: 'Checkbox 6 Checkbox 6 Checkbox 6 Checkbox 6 Checkbox 6 Checkbox 6 Checkbox 6 Checkbox 6 Checkbox 6 Checkbox 6',
value: 'value6',
handler: () => {
console.log('Checkbox 6 selected');
}
}
],
buttons: [
{
text: 'Cancel',
role: 'cancel',
cssClass: 'secondary',
handler: () => {
console.log('Confirm Cancel');
}
}, {
text: 'Ok',
handler: () => {
console.log('Confirm Ok');
}
}
]
});
await alert.present();
}
render() {
return [
<ion-content>
<ion-button onClick={() => this.presentAlert()}>Present Alert</ion-button>
<ion-button onClick={() => this.presentAlertMultipleButtons()}>Present Alert: Multiple Buttons</ion-button>
<ion-button onClick={() => this.presentAlertConfirm()}>Present Alert: Confirm</ion-button>
<ion-button onClick={() => this.presentAlertPrompt()}>Present Alert: Prompt</ion-button>
<ion-button onClick={() => this.presentAlertRadio()}>Present Alert: Radio</ion-button>
<ion-button onClick={() => this.presentAlertCheckbox()}>Present Alert: Checkbox</ion-button>
</ion-content>
];
}
<template>
<ion-button @click="presentAlert">Show Alert</ion-button>
<ion-button @click="presentAlertMultipleButtons">Show Alert (multiple buttons)</ion-button>
<ion-button @click="presentAlertConfirm">Show Alert (confirm)</ion-button>
<ion-button @click="presentAlertPrompt">Show Alert (prompt)</ion-button>
<ion-button @click="presentAlertRadio">Show Alert (radio)</ion-button>
<ion-button @click="presentAlertCheckbox">Show Alert (checkbox)</ion-button>
</template>
<script>
import { IonButton, alertController } from '@ionic/vue';
import { defineComponent } from 'vue';
export default defineComponent({
components: { IonButton },
methods: {
async presentAlert() {
const alert = await alertController
.create({
cssClass: 'my-custom-class',
header: 'Alert',
subHeader: 'Subtitle',
message: 'This is an alert message.',
buttons: ['OK'],
});
await alert.present();
const { role } = await alert.onDidDismiss();
console.log('onDidDismiss resolved with role', role);
},
async presentAlertMultipleButtons() {
const alert = await alertController
.create({
cssClass: 'my-custom-class',
header: 'Alert',
subHeader: 'Subtitle',
message: 'This is an alert message.',
buttons: ['Cancel', 'Open Modal', 'Delete'],
});
return alert.present();
},
async presentAlertConfirm() {
const alert = await alertController
.create({
cssClass: 'my-custom-class',
header: 'Confirm!',
message: 'Message <strong>text</strong>!!!',
buttons: [
{
text: 'Cancel',
role: 'cancel',
cssClass: 'secondary',
handler: blah => {
console.log('Confirm Cancel:', blah)
},
},
{
text: 'Okay',
handler: () => {
console.log('Confirm Okay')
},
},
],
});
return alert.present();
},
async presentAlertPrompt() {
const alert = await alertController
.create({
cssClass: 'my-custom-class',
header: 'Prompt!',
inputs: [
{
placeholder: 'Placeholder 1',
},
{
name: 'name2',
id: 'name2-id',
value: 'hello',
placeholder: 'Placeholder 2',
},
{
name: 'name3',
value: 'http://ionicframework.com',
type: 'url',
placeholder: 'Favorite site ever',
},
// input date with min & max
{
name: 'name4',
type: 'date',
min: '2017-03-01',
max: '2018-01-12',
},
// input date without min nor max
{
name: 'name5',
type: 'date',
},
{
name: 'name6',
type: 'number',
min: -5,
max: 10,
},
{
name: 'name7',
type: 'number',
},
{
name: 'name8',
type: 'password',
placeholder: 'Advanced Attributes',
cssClass: 'specialClass',
attributes: {
maxlength: 4,
inputmode: 'decimal'
}
}
],
buttons: [
{
text: 'Cancel',
role: 'cancel',
cssClass: 'secondary',
handler: () => {
console.log('Confirm Cancel')
},
},
{
text: 'Ok',
handler: () => {
console.log('Confirm Ok')
},
},
],
});
return alert.present();
},
async presentAlertRadio() {
const alert = await alertController
.create({
cssClass: 'my-custom-class',
header: 'Radio',
inputs: [
{
type: 'radio',
label: 'Radio 1',
value: 'value1',
handler: () => {
console.log('Radio 1 selected');
},
checked: true,
},
{
type: 'radio',
label: 'Radio 2',
value: 'value2',
handler: () => {
console.log('Radio 2 selected');
}
},
{
type: 'radio',
label: 'Radio 3',
value: 'value3',
handler: () => {
console.log('Radio 3 selected');
}
},
{
type: 'radio',
label: 'Radio 4',
value: 'value4',
handler: () => {
console.log('Radio 4 selected');
}
},
{
type: 'radio',
label: 'Radio 5',
value: 'value5',
handler: () => {
console.log('Radio 5 selected');
}
},
{
type: 'radio',
label: 'Radio 6',
value: 'value6',
handler: () => {
console.log('Radio 6 selected');
}
},
],
buttons: [
{
text: 'Cancel',
role: 'cancel',
cssClass: 'secondary',
handler: () => {
console.log('Confirm Cancel')
},
},
{
text: 'Ok',
handler: () => {
console.log('Confirm Ok')
},
},
],
});
return alert.present();
},
async presentAlertCheckbox() {
const alert = await alertController
.create({
cssClass: 'my-custom-class',
header: 'Checkbox',
inputs: [
{
type: 'checkbox',
label: 'Checkbox 1',
value: 'value1',
handler: () => {
console.log('Checkbox 1 selected');
},
checked: true,
},
{
type: 'checkbox',
label: 'Checkbox 2',
value: 'value2',
handler: () => {
console.log('Checkbox 2 selected');
}
},
{
type: 'checkbox',
label: 'Checkbox 3',
value: 'value3',
handler: () => {
console.log('Checkbox 3 selected');
}
},
{
type: 'checkbox',
label: 'Checkbox 4',
value: 'value4',
handler: () => {
console.log('Checkbox 4 selected');
}
},
{
type: 'checkbox',
label: 'Checkbox 5',
value: 'value5',
handler: () => {
console.log('Checkbox 5 selected');
}
},
{
type: 'checkbox',
label: 'Checkbox 6',
value: 'value6',
handler: () => {
console.log('Checkbox 6 selected');
}
},
],
buttons: [
{
text: 'Cancel',
role: 'cancel',
cssClass: 'secondary',
handler: () => {
console.log('Confirm Cancel')
},
},
{
text: 'Ok',
handler: () => {
console.log('Confirm Ok')
},
},
],
});
return alert.present();
},
},
});
</script>
CopyCopiedDevelopers can also use this component directly in their template:
<template>
<ion-button @click="setOpen(true)">Show Alert</ion-button>
<ion-alert
:is-open="isOpenRef"
header="Alert"
sub-header="Subtitle"
message="This is an alert message."
css-class="my-custom-class"
:buttons="buttons"
@onDidDismiss="setOpen(false)"
>
</ion-alert>
</template>
<script>
import { IonAlert, IonButton } from '@ionic/vue';
import { defineComponent, ref } from 'vue';
export default defineComponent({
components: { IonAlert, IonButton },
setup() {
const isOpenRef = ref(false);
const setOpen = (state: boolean) => isOpenRef.value = state;
const buttons = ['Ok'];
return { buttons, isOpenRef, setOpen }
}
});
</script>
function presentAlert() {
const alert = document.createElement('ion-alert');
alert.cssClass = 'my-custom-class';
alert.header = 'Alert';
alert.subHeader = 'Subtitle';
alert.message = 'This is an alert message.';
alert.buttons = ['OK'];
document.body.appendChild(alert);
await alert.present();
const { role } = await alert.onDidDismiss();
console.log('onDidDismiss resolved with role', role);
}
function presentAlertMultipleButtons() {
const alert = document.createElement('ion-alert');
alert.cssClass = 'my-custom-class';
alert.header = 'Alert';
alert.subHeader = 'Subtitle';
alert.message = 'This is an alert message.';
alert.buttons = ['Cancel', 'Open Modal', 'Delete'];
document.body.appendChild(alert);
return alert.present();
}
function presentAlertConfirm() {
const alert = document.createElement('ion-alert');
alert.cssClass = 'my-custom-class';
alert.header = 'Confirm!';
alert.message = 'Message <strong>text</strong>!!!';
alert.buttons = [
{
text: 'Cancel',
role: 'cancel',
cssClass: 'secondary',
handler: (blah) => {
console.log('Confirm Cancel: blah');
}
}, {
text: 'Okay',
handler: () => {
console.log('Confirm Okay')
}
}
];
document.body.appendChild(alert);
return alert.present();
}
function presentAlertPrompt() {
const alert = document.createElement('ion-alert');
alert.cssClass = 'my-custom-class';
alert.header = 'Prompt!';
alert.inputs = [
{
placeholder: 'Placeholder 1'
},
{
name: 'name2',
id: 'name2-id',
value: 'hello',
placeholder: 'Placeholder 2'
},
// multiline input.
{
name: 'paragraph',
id: 'paragraph',
type: 'textarea',
placeholder: 'Placeholder 3'
},
{
name: 'name3',
value: 'http://ionicframework.com',
type: 'url',
placeholder: 'Favorite site ever'
},
// input date with min & max
{
name: 'name4',
type: 'date',
min: '2017-03-01',
max: '2018-01-12'
},
// input date without min nor max
{
name: 'name5',
type: 'date'
},
{
name: 'name6',
type: 'number',
min: -5,
max: 10
},
{
name: 'name7',
type: 'number'
},
{
name: 'name8',
type: 'password',
placeholder: 'Advanced Attributes',
cssClass: 'specialClass',
attributes: {
maxlength: 4,
inputmode: 'decimal'
}
}
];
alert.buttons = [
{
text: 'Cancel',
role: 'cancel',
cssClass: 'secondary',
handler: () => {
console.log('Confirm Cancel')
}
}, {
text: 'Ok',
handler: () => {
console.log('Confirm Ok')
}
}
];
document.body.appendChild(alert);
return alert.present();
}
function presentAlertRadio() {
const alert = document.createElement('ion-alert');
alert.cssClass = 'my-custom-class';
alert.header = 'Radio';
alert.inputs = [
{
type: 'radio',
label: 'Radio 1',
value: 'value1',
handler: () => {
console.log('Radio 1 selected');
},
checked: true
},
{
type: 'radio',
label: 'Radio 2',
value: 'value2',
handler: () => {
console.log('Radio 2 selected');
}
},
{
type: 'radio',
label: 'Radio 3',
value: 'value3',
handler: () => {
console.log('Radio 3 selected');
}
},
{
type: 'radio',
label: 'Radio 4',
value: 'value4',
handler: () => {
console.log('Radio 4 selected');
}
},
{
type: 'radio',
label: 'Radio 5',
value: 'value5',
handler: () => {
console.log('Radio 5 selected');
}
},
{
type: 'radio',
label: 'Radio 6 Radio 6 Radio 6 Radio 6 Radio 6 Radio 6 Radio 6 Radio 6 Radio 6 Radio 6 ',
value: 'value6',
handler: () => {
console.log('Radio 6 selected');
}
}
];
alert.buttons = [
{
text: 'Cancel',
role: 'cancel',
cssClass: 'secondary',
handler: () => {
console.log('Confirm Cancel')
}
}, {
text: 'Ok',
handler: () => {
console.log('Confirm Ok')
}
}
];
document.body.appendChild(alert);
return alert.present();
}
function presentAlertCheckbox() {
const alert = document.createElement('ion-alert');
alert.cssClass = 'my-custom-class';
alert.header = 'Checkbox';
alert.inputs = [
{
type: 'checkbox',
label: 'Checkbox 1',
value: 'value1',
handler: () => {
console.log('Checkbox 1 selected');
},
checked: true
},
{
type: 'checkbox',
label: 'Checkbox 2',
value: 'value2',
handler: () => {
console.log('Checkbox 2 selected');
}
},
{
type: 'checkbox',
label: 'Checkbox 3',
value: 'value3',
handler: () => {
console.log('Checkbox 3 selected');
}
},
{
type: 'checkbox',
label: 'Checkbox 4',
value: 'value4',
handler: () => {
console.log('Checkbox 4 selected');
}
},
{
type: 'checkbox',
label: 'Checkbox 5',
value: 'value5',
handler: () => {
console.log('Checkbox 5 selected');
}
},
{
type: 'checkbox',
label: 'Checkbox 6 Checkbox 6 Checkbox 6 Checkbox 6 Checkbox 6 Checkbox 6 Checkbox 6 Checkbox 6 Checkbox 6 Checkbox 6',
value: 'value6',
handler: () => {
console.log('Checkbox 6 selected');
}
}
];
alert.buttons = [
{
text: 'Cancel',
role: 'cancel',
cssClass: 'secondary',
handler: () => {
console.log('Confirm Cancel')
}
}, {
text: 'Ok',
handler: () => {
console.log('Confirm Ok')
}
}
];
document.body.appendChild(alert);
return alert.present();
}/* Using with useIonAlert Hook */
import React from 'react';
import { IonButton, IonContent, IonPage, useIonAlert } from '@ionic/react';
const AlertExample: React.FC = () => {
const [present] = useIonAlert();
return (
<IonPage>
<IonContent fullscreen>
<IonButton
expand="block"
onClick={() =>
present({
cssClass: 'my-css',
header: 'Alert',
message: 'alert from hook',
buttons: [
'Cancel',
{ text: 'Ok', handler: (d) => console.log('ok pressed') },
],
onDidDismiss: (e) => console.log('did dismiss'),
})
}
>
Show Alert
</IonButton>
<IonButton
expand="block"
onClick={() => present('hello with params', [{ text: 'Ok' }])}
>
Show Alert using params
</IonButton>
</IonContent>
</IonPage>
);
};
CopyCopied/* Using with IonAlert Component */
import React, { useState } from 'react';
import { IonAlert, IonButton, IonContent } from '@ionic/react';
export const AlertExample: React.FC = () => {
const [showAlert1, setShowAlert1] = useState(false);
const [showAlert2, setShowAlert2] = useState(false);
const [showAlert3, setShowAlert3] = useState(false);
const [showAlert4, setShowAlert4] = useState(false);
const [showAlert5, setShowAlert5] = useState(false);
const [showAlert6, setShowAlert6] = useState(false);
return (
<IonContent>
<IonButton onClick={() => setShowAlert1(true)} expand="block">Show Alert 1</IonButton>
<IonButton onClick={() => setShowAlert2(true)} expand="block">Show Alert 2</IonButton>
<IonButton onClick={() => setShowAlert3(true)} expand="block">Show Alert 3</IonButton>
<IonButton onClick={() => setShowAlert4(true)} expand="block">Show Alert 4</IonButton>
<IonButton onClick={() => setShowAlert5(true)} expand="block">Show Alert 5</IonButton>
<IonButton onClick={() => setShowAlert6(true)} expand="block">Show Alert 6</IonButton>
<IonAlert
isOpen={showAlert1}
onDidDismiss={() => setShowAlert1(false)}
cssClass='my-custom-class'
header={'Alert'}
subHeader={'Subtitle'}
message={'This is an alert message.'}
buttons={['OK']}
/>
<IonAlert
isOpen={showAlert2}
onDidDismiss={() => setShowAlert2(false)}
cssClass='my-custom-class'
header={'Alert'}
subHeader={'Subtitle'}
message={'This is an alert message.'}
buttons={['Cancel', 'Open Modal', 'Delete']}
/>
<IonAlert
isOpen={showAlert3}
onDidDismiss={() => setShowAlert3(false)}
cssClass='my-custom-class'
header={'Confirm!'}
message={'Message <strong>text</strong>!!!'}
buttons={[
{
text: 'Cancel',
role: 'cancel',
cssClass: 'secondary',
handler: blah => {
console.log('Confirm Cancel: blah');
}
},
{
text: 'Okay',
handler: () => {
console.log('Confirm Okay');
}
}
]}
/>
<IonAlert
isOpen={showAlert4}
onDidDismiss={() => setShowAlert4(false)}
cssClass='my-custom-class'
header={'Prompt!'}
inputs={[
{
name: 'name1',
type: 'text',
placeholder: 'Placeholder 1'
},
{
name: 'name2',
type: 'text',
id: 'name2-id',
value: 'hello',
placeholder: 'Placeholder 2'
},
{
name: 'name3',
value: 'http://ionicframework.com',
type: 'url',
placeholder: 'Favorite site ever'
},
// input date with min & max
{
name: 'name4',
type: 'date',
min: '2017-03-01',
max: '2018-01-12'
},
// input date without min nor max
{
name: 'name5',
type: 'date'
},
{
name: 'name6',
type: 'number',
min: -5,
max: 10
},
{
name: 'name7',
type: 'number'
},
{
name: 'name8',
type: 'password',
placeholder: 'Advanced Attributes',
cssClass: 'specialClass',
attributes: {
maxlength: 4,
inputmode: 'decimal'
}
}
]}
buttons={[
{
text: 'Cancel',
role: 'cancel',
cssClass: 'secondary',
handler: () => {
console.log('Confirm Cancel');
}
},
{
text: 'Ok',
handler: () => {
console.log('Confirm Ok');
}
}
]}
/>
<IonAlert
isOpen={showAlert5}
onDidDismiss={() => setShowAlert5(false)}
cssClass='my-custom-class'
header={'Radio'}
inputs={[
{
name: 'radio1',
type: 'radio',
label: 'Radio 1',
value: 'value1',
handler: () => {
console.log('Radio 1 selected');
},
checked: true
},
{
name: 'radio2',
type: 'radio',
label: 'Radio 2',
value: 'value2',
handler: () => {
console.log('Radio 2 selected');
}
},
{
name: 'radio3',
type: 'radio',
label: 'Radio 3',
value: 'value3',
handler: () => {
console.log('Radio 3 selected');
}
},
{
name: 'radio4',
type: 'radio',
label: 'Radio 4',
value: 'value4',
handler: () => {
console.log('Radio 4 selected');
}
},
{
name: 'radio5',
type: 'radio',
label: 'Radio 5',
value: 'value5',
handler: () => {
console.log('Radio 5 selected');
}
},
{
name: 'radio6',
type: 'radio',
label: 'Radio 6',
value: 'value6',
handler: () => {
console.log('Radio 6 selected');
}
}
]}
buttons={[
{
text: 'Cancel',
role: 'cancel',
cssClass: 'secondary',
handler: () => {
console.log('Confirm Cancel');
}
},
{
text: 'Ok',
handler: () => {
console.log('Confirm Ok');
}
}
]}
/>
<IonAlert
isOpen={showAlert6}
onDidDismiss={() => setShowAlert6(false)}
cssClass='my-custom-class'
header={'Checkbox'}
inputs={[
{
name: 'checkbox1',
type: 'checkbox',
label: 'Checkbox 1',
value: 'value1',
handler: () => {
console.log('Checkbox 1 selected');
},
checked: true
},
{
name: 'checkbox2',
type: 'checkbox',
label: 'Checkbox 2',
value: 'value2',
handler: () => {
console.log('Checkbox 2 selected');
}
},
{
name: 'checkbox3',
type: 'checkbox',
label: 'Checkbox 3',
value: 'value3',
handler: () => {
console.log('Checkbox 3 selected');
}
},
{
name: 'checkbox4',
type: 'checkbox',
label: 'Checkbox 4',
value: 'value4',
handler: () => {
console.log('Checkbox 4 selected');
}
},
{
name: 'checkbox5',
type: 'checkbox',
label: 'Checkbox 5',
value: 'value5',
handler: () => {
console.log('Checkbox 5 selected');
}
},
{
name: 'checkbox6',
type: 'checkbox',
label: 'Checkbox 6',
value: 'value6',
handler: () => {
console.log('Checkbox 6 selected');
}
}
]}
buttons={[
{
text: 'Cancel',
role: 'cancel',
cssClass: 'secondary',
handler: () => {
console.log('Confirm Cancel');
}
},
{
text: 'Ok',
handler: () => {
console.log('Confirm Ok');
}
}
]}
/>
</IonContent>
);
}
export default AlertExample;
import { Component, h } from '@stencil/core';
import { alertController } from '@ionic/core';
@Component({
tag: 'alert-example',
styleUrl: 'alert-example.css'
})
export class AlertExample {
async presentAlert() {
const alert = await alertController.create({
cssClass: 'my-custom-class',
header: 'Alert',
subHeader: 'Subtitle',
message: 'This is an alert message.',
buttons: ['OK']
});
await alert.present();
const { role } = await alert.onDidDismiss();
console.log('onDidDismiss resolved with role', role);
}
async presentAlertMultipleButtons() {
const alert = await alertController.create({
cssClass: 'my-custom-class',
header: 'Alert',
subHeader: 'Subtitle',
message: 'This is an alert message.',
buttons: ['Cancel', 'Open Modal', 'Delete']
});
await alert.present();
}
async presentAlertConfirm() {
const alert = await alertController.create({
cssClass: 'my-custom-class',
header: 'Confirm!',
message: 'Message <strong>text</strong>!!!',
buttons: [
{
text: 'Cancel',
role: 'cancel',
cssClass: 'secondary',
handler: (blah) => {
console.log('Confirm Cancel: blah');
}
}, {
text: 'Okay',
handler: () => {
console.log('Confirm Okay');
}
}
]
});
await alert.present();
}
async presentAlertPrompt() {
const alert = await alertController.create({
cssClass: 'my-custom-class',
header: 'Prompt!',
inputs: [
{
name: 'name1',
type: 'text',
placeholder: 'Placeholder 1'
},
{
name: 'name2',
type: 'text',
id: 'name2-id',
value: 'hello',
placeholder: 'Placeholder 2'
},
// multiline input.
{
name: 'paragraph',
id: 'paragraph',
type: 'textarea',
placeholder: 'Placeholder 3'
},
{
name: 'name3',
value: 'http://ionicframework.com',
type: 'url',
placeholder: 'Favorite site ever'
},
// input date with min & max
{
name: 'name4',
type: 'date',
min: '2017-03-01',
max: '2018-01-12'
},
// input date without min nor max
{
name: 'name5',
type: 'date'
},
{
name: 'name6',
type: 'number',
min: -5,
max: 10
},
{
name: 'name7',
type: 'number'
},
{
name: 'name8',
type: 'password',
placeholder: 'Advanced Attributes',
cssClass: 'specialClass',
attributes: {
maxlength: 4,
inputmode: 'decimal'
}
}
],
buttons: [
{
text: 'Cancel',
role: 'cancel',
cssClass: 'secondary',
handler: () => {
console.log('Confirm Cancel');
}
}, {
text: 'Ok',
handler: () => {
console.log('Confirm Ok');
}
}
]
});
await alert.present();
}
async presentAlertRadio() {
const alert = await alertController.create({
cssClass: 'my-custom-class',
header: 'Radio',
inputs: [
{
name: 'radio1',
type: 'radio',
label: 'Radio 1',
value: 'value1',
handler: () => {
console.log('Radio 1 selected');
},
checked: true
},
{
name: 'radio2',
type: 'radio',
label: 'Radio 2',
value: 'value2',
handler: () => {
console.log('Radio 2 selected');
}
},
{
name: 'radio3',
type: 'radio',
label: 'Radio 3',
value: 'value3',
handler: () => {
console.log('Radio 3 selected');
}
},
{
name: 'radio4',
type: 'radio',
label: 'Radio 4',
value: 'value4',
handler: () => {
console.log('Radio 4 selected');
}
},
{
name: 'radio5',
type: 'radio',
label: 'Radio 5',
value: 'value5',
handler: () => {
console.log('Radio 5 selected');
}
},
{
name: 'radio6',
type: 'radio',
label: 'Radio 6 Radio 6 Radio 6 Radio 6 Radio 6 Radio 6 Radio 6 Radio 6 Radio 6 Radio 6 ',
value: 'value6',
handler: () => {
console.log('Radio 6 selected');
}
}
],
buttons: [
{
text: 'Cancel',
role: 'cancel',
cssClass: 'secondary',
handler: () => {
console.log('Confirm Cancel');
}
}, {
text: 'Ok',
handler: () => {
console.log('Confirm Ok');
}
}
]
});
await alert.present();
}
async presentAlertCheckbox() {
const alert = await alertController.create({
cssClass: 'my-custom-class',
header: 'Checkbox',
inputs: [
{
name: 'checkbox1',
type: 'checkbox',
label: 'Checkbox 1',
value: 'value1',
handler: () => {
console.log('Checkbox 1 selected');
},
checked: true
},
{
name: 'checkbox2',
type: 'checkbox',
label: 'Checkbox 2',
value: 'value2',
handler: () => {
console.log('Checkbox 2 selected');
}
},
{
name: 'checkbox3',
type: 'checkbox',
label: 'Checkbox 3',
value: 'value3',
handler: () => {
console.log('Checkbox 3 selected');
}
},
{
name: 'checkbox4',
type: 'checkbox',
label: 'Checkbox 4',
value: 'value4',
handler: () => {
console.log('Checkbox 4 selected');
}
},
{
name: 'checkbox5',
type: 'checkbox',
label: 'Checkbox 5',
value: 'value5',
handler: () => {
console.log('Checkbox 5 selected');
}
},
{
name: 'checkbox6',
type: 'checkbox',
label: 'Checkbox 6 Checkbox 6 Checkbox 6 Checkbox 6 Checkbox 6 Checkbox 6 Checkbox 6 Checkbox 6 Checkbox 6 Checkbox 6',
value: 'value6',
handler: () => {
console.log('Checkbox 6 selected');
}
}
],
buttons: [
{
text: 'Cancel',
role: 'cancel',
cssClass: 'secondary',
handler: () => {
console.log('Confirm Cancel');
}
}, {
text: 'Ok',
handler: () => {
console.log('Confirm Ok');
}
}
]
});
await alert.present();
}
render() {
return [
<ion-content>
<ion-button onClick={() => this.presentAlert()}>Present Alert</ion-button>
<ion-button onClick={() => this.presentAlertMultipleButtons()}>Present Alert: Multiple Buttons</ion-button>
<ion-button onClick={() => this.presentAlertConfirm()}>Present Alert: Confirm</ion-button>
<ion-button onClick={() => this.presentAlertPrompt()}>Present Alert: Prompt</ion-button>
<ion-button onClick={() => this.presentAlertRadio()}>Present Alert: Radio</ion-button>
<ion-button onClick={() => this.presentAlertCheckbox()}>Present Alert: Checkbox</ion-button>
</ion-content>
];
}
<template>
<ion-button @click="presentAlert">Show Alert</ion-button>
<ion-button @click="presentAlertMultipleButtons">Show Alert (multiple buttons)</ion-button>
<ion-button @click="presentAlertConfirm">Show Alert (confirm)</ion-button>
<ion-button @click="presentAlertPrompt">Show Alert (prompt)</ion-button>
<ion-button @click="presentAlertRadio">Show Alert (radio)</ion-button>
<ion-button @click="presentAlertCheckbox">Show Alert (checkbox)</ion-button>
</template>
<script>
import { IonButton, alertController } from '@ionic/vue';
import { defineComponent } from 'vue';
export default defineComponent({
components: { IonButton },
methods: {
async presentAlert() {
const alert = await alertController
.create({
cssClass: 'my-custom-class',
header: 'Alert',
subHeader: 'Subtitle',
message: 'This is an alert message.',
buttons: ['OK'],
});
await alert.present();
const { role } = await alert.onDidDismiss();
console.log('onDidDismiss resolved with role', role);
},
async presentAlertMultipleButtons() {
const alert = await alertController
.create({
cssClass: 'my-custom-class',
header: 'Alert',
subHeader: 'Subtitle',
message: 'This is an alert message.',
buttons: ['Cancel', 'Open Modal', 'Delete'],
});
return alert.present();
},
async presentAlertConfirm() {
const alert = await alertController
.create({
cssClass: 'my-custom-class',
header: 'Confirm!',
message: 'Message <strong>text</strong>!!!',
buttons: [
{
text: 'Cancel',
role: 'cancel',
cssClass: 'secondary',
handler: blah => {
console.log('Confirm Cancel:', blah)
},
},
{
text: 'Okay',
handler: () => {
console.log('Confirm Okay')
},
},
],
});
return alert.present();
},
async presentAlertPrompt() {
const alert = await alertController
.create({
cssClass: 'my-custom-class',
header: 'Prompt!',
inputs: [
{
placeholder: 'Placeholder 1',
},
{
name: 'name2',
id: 'name2-id',
value: 'hello',
placeholder: 'Placeholder 2',
},
{
name: 'name3',
value: 'http://ionicframework.com',
type: 'url',
placeholder: 'Favorite site ever',
},
// input date with min & max
{
name: 'name4',
type: 'date',
min: '2017-03-01',
max: '2018-01-12',
},
// input date without min nor max
{
name: 'name5',
type: 'date',
},
{
name: 'name6',
type: 'number',
min: -5,
max: 10,
},
{
name: 'name7',
type: 'number',
},
{
name: 'name8',
type: 'password',
placeholder: 'Advanced Attributes',
cssClass: 'specialClass',
attributes: {
maxlength: 4,
inputmode: 'decimal'
}
}
],
buttons: [
{
text: 'Cancel',
role: 'cancel',
cssClass: 'secondary',
handler: () => {
console.log('Confirm Cancel')
},
},
{
text: 'Ok',
handler: () => {
console.log('Confirm Ok')
},
},
],
});
return alert.present();
},
async presentAlertRadio() {
const alert = await alertController
.create({
cssClass: 'my-custom-class',
header: 'Radio',
inputs: [
{
type: 'radio',
label: 'Radio 1',
value: 'value1',
handler: () => {
console.log('Radio 1 selected');
},
checked: true,
},
{
type: 'radio',
label: 'Radio 2',
value: 'value2',
handler: () => {
console.log('Radio 2 selected');
}
},
{
type: 'radio',
label: 'Radio 3',
value: 'value3',
handler: () => {
console.log('Radio 3 selected');
}
},
{
type: 'radio',
label: 'Radio 4',
value: 'value4',
handler: () => {
console.log('Radio 4 selected');
}
},
{
type: 'radio',
label: 'Radio 5',
value: 'value5',
handler: () => {
console.log('Radio 5 selected');
}
},
{
type: 'radio',
label: 'Radio 6',
value: 'value6',
handler: () => {
console.log('Radio 6 selected');
}
},
],
buttons: [
{
text: 'Cancel',
role: 'cancel',
cssClass: 'secondary',
handler: () => {
console.log('Confirm Cancel')
},
},
{
text: 'Ok',
handler: () => {
console.log('Confirm Ok')
},
},
],
});
return alert.present();
},
async presentAlertCheckbox() {
const alert = await alertController
.create({
cssClass: 'my-custom-class',
header: 'Checkbox',
inputs: [
{
type: 'checkbox',
label: 'Checkbox 1',
value: 'value1',
handler: () => {
console.log('Checkbox 1 selected');
},
checked: true,
},
{
type: 'checkbox',
label: 'Checkbox 2',
value: 'value2',
handler: () => {
console.log('Checkbox 2 selected');
}
},
{
type: 'checkbox',
label: 'Checkbox 3',
value: 'value3',
handler: () => {
console.log('Checkbox 3 selected');
}
},
{
type: 'checkbox',
label: 'Checkbox 4',
value: 'value4',
handler: () => {
console.log('Checkbox 4 selected');
}
},
{
type: 'checkbox',
label: 'Checkbox 5',
value: 'value5',
handler: () => {
console.log('Checkbox 5 selected');
}
},
{
type: 'checkbox',
label: 'Checkbox 6',
value: 'value6',
handler: () => {
console.log('Checkbox 6 selected');
}
},
],
buttons: [
{
text: 'Cancel',
role: 'cancel',
cssClass: 'secondary',
handler: () => {
console.log('Confirm Cancel')
},
},
{
text: 'Ok',
handler: () => {
console.log('Confirm Ok')
},
},
],
});
return alert.present();
},
},
});
</script>
CopyCopiedDevelopers can also use this component directly in their template:
<template>
<ion-button @click="setOpen(true)">Show Alert</ion-button>
<ion-alert
:is-open="isOpenRef"
header="Alert"
sub-header="Subtitle"
message="This is an alert message."
css-class="my-custom-class"
:buttons="buttons"
@onDidDismiss="setOpen(false)"
>
</ion-alert>
</template>
<script>
import { IonAlert, IonButton } from '@ionic/vue';
import { defineComponent, ref } from 'vue';
export default defineComponent({
components: { IonAlert, IonButton },
setup() {
const isOpenRef = ref(false);
const setOpen = (state: boolean) => isOpenRef.value = state;
const buttons = ['Ok'];
return { buttons, isOpenRef, setOpen }
}
});
</script>
Style Placement
Trong Angular, CSS của một trang cụ thể chỉ được áp dụng cho các phần tử của trang đó. Mặc dù Cảnh báo có thể được hiển thị từ bên trong một trang, ion-alertphần tử được nối bên ngoài trang hiện tại. Điều này có nghĩa là bất kỳ kiểu tùy chỉnh nào cũng cần phải đi trong tệp biểu định kiểu chung. Trong bộ khởi động Ionic Angular, đây có thể là src/global.scsstệp hoặc bạn có thể đăng ký tệp kiểu toàn cục mới bằng cách thêm vào styles tùy chọn xây dựng trongangular.json .
Properties
animated | |
|---|---|
| Description | If |
| Attribute | animated |
| Type | boolean |
| Default | true |
backdropDismiss | |
| Description | If |
| Attribute | backdrop-dismiss |
| Type | boolean |
| Default | true |
buttons | |
| Description | Array of buttons to be added to the alert. |
| Type | (string | AlertButton)[] |
| Default | [] |
cssClass | |
| Description | Additional classes to apply for custom CSS. If multiple classes are provided they should be separated by spaces. |
| Attribute | css-class |
| Type | string | string[] | undefined |
enterAnimation | |
| Description | Animation to use when the alert is presented. |
| Type | ((baseEl: any, opts?: any) => Animation) | undefined |
header | |
| Description | The main title in the heading of the alert. |
| Attribute | header |
| Type | string | undefined |
inputs | |
| Description | Array of input to show in the alert. |
| Type | AlertInput[] |
| Default | [] |
keyboardClose | |
| Description | If |
| Attribute | keyboard-close |
| Type | boolean |
| Default | true |
leaveAnimation | |
| Description | Animation to use when the alert is dismissed. |
| Type | ((baseEl: any, opts?: any) => Animation) | undefined |
message | |
| Description | The main message to be displayed in the alert. For more information: Security Documentation |
| Attribute | message |
| Type | IonicSafeString | string | undefined |
mode | |
| Description | The mode determines which platform styles to use. |
| Attribute | mode |
| Type | "ios" | "md" |
subHeader | |
| Description | The subtitle in the heading of the alert. Displayed under the title. |
| Attribute | sub-header |
| Type | string | undefined |
translucent | |
| Description | If |
| Attribute | translucent |
| Type | boolean |
| Default | false |
Events
| Name | Description |
|---|---|
ionAlertDidDismiss | Emitted after the alert has dismissed. |
ionAlertDidPresent | Emitted after the alert has presented. |
ionAlertWillDismiss | Emitted before the alert has dismissed. |
ionAlertWillPresent | Emitted before the alert has presented. |
methods
dismiss | |
|---|---|
| Description | Dismiss the alert overlay after it has been presented. |
| Signature | dismiss(data?: any, role?: string | undefined) => Promise<boolean> |
onDidDismiss | |
| Description | Returns a promise that resolves when the alert did dismiss. |
| Signature | onDidDismiss<T = any>() => Promise<OverlayEventDetail<T>> |
onWillDismiss | |
| Description | Returns a promise that resolves when the alert will dismiss. |
| Signature | onWillDismiss<T = any>() => Promise<OverlayEventDetail<T>> |
present | |
| Description | Present the alert overlay after it has been created. |
| Signature | present() => Promise<void> |
Css Custom Propreties
| Name | Description |
|---|---|
--backdrop-opacity | Opacity of the backdrop |
--background | Background of the alert |
--height | Height of the alert |
--max-height | Maximum height of the alert |
--max-width | Maximum width of the alert |
--min-height | Minimum height of the alert |
--min-width | Minimum width of the alert |
--width | Width of the alert |

0 Nhận xét