1.导入:
import { AlertController } from '@ionic/angular';
2.在沟槽函数里面声明:
constructor(public alertController: AlertController) {}
3.添加几个点击按纽:
<ion-button (click)="presentAlert()">
支付
</ion-button>
<ion-button (click)="presentAlertMultipleButtons()">
删除
</ion-button>
<ion-button (click)="presentAlertPrompt()">
添加信息
</ion-button>
4.点击事件里面写代码:
async presentAlert() {
const alert = await this.alertController.create({
header: '提示信息',
message: '支付成功',
buttons: ['OK']
});
await alert.present();
}
async presentAlertMultipleButtons() {
const alert = await this.alertController.create({
header: '提示信息!',
message: '您确定要删除吗',
buttons: [
{
text: '取消',
role: 'cancel',
cssClass: 'secondary',
handler: (blah) => {
console.log('Confirm Cancel: blah');
}
}, {
text: '确定',
handler: () => {
console.log('Confirm Okay');
}
}
]
});
await alert.present();
}
async presentAlertPrompt() {
const alert = await this.alertController.create({
header: '请输入信息',
inputs: [
{
name: '姓名',
type: 'text',
placeholder: '请输入姓名'
},
{
name: '卡号',
type: 'text',
id: 'name2-id',
value: 'hello',
placeholder: '请输入卡号'
},
{
name: '密码',
type: 'url',
placeholder: '请输入密码'
},
// input date with min & max
{
name: '出生日期',
type: 'date',
min: '1900-03-01',
max: '2019-08-25'
},
// input date without min nor max
{
name: '年龄',
type: 'number',
min: 0,
max: 109,
placeholder: '请输入年龄'
},
{
name: '手机号',
type: 'number',
placeholder: '请输入手机号'
}
],
buttons: [
{
text: '取消',
role: 'cancel',
cssClass: 'secondary',
handler: () => {
console.log('Confirm Cancel');
}
}, {
text: '确定',
handler: () => {
console.log('Confirm Ok');
}
}
]
});
await alert.present();
}