这是警报的共享提供程序
共享.provider.ts
import { Injectable } from '@angular/core';
import { AlertController } from 'ionic-angular';
@Injectable()
export class SharedProvider {
constructor(private _alert: AlertController) { }
public Alert = {
confirm: (msg?, title?) => {
return new Promise((resolve, reject) => {
let alert = this._alert.create({
title: title || 'Confirm',
message: msg || 'Do you want continue?',
buttons: [
{
text: 'Cancel',
role: 'cancel',
handler: () => {
reject(false);
}
},
{
text: 'Ok',
handler: () => {
resolve(true);
}
}
]
});
alert.present();
});
},
alert: (msg, title?) => {
let alert = this._alert.create({
title: title || 'Alert',
subTitle: msg,
buttons: ['Dismiss']
});
alert.present();
}
}
}
Usage
Home.ts
import { SharedProvider } from '../../providers/shared.provider';
@Component({
selector: 'page-home',
templateUrl: 'home.html',
providers: [SharedProvider]
})
export class HomePage {
constructor(public shared: SharedProvider) {}
deletePost(gossip) {
this.shared.Alert.confirm().then((res) => {
console.log('confirmed');
}, err => {
console.log('user cancelled');
})
}
}
您可以添加更多常用功能。就像 toast 味精添加一样 -
public Toast = {
show: (text: string, duration?, position?, closeButton?, btnText?) => {
this._toastMsg = this._toastCtrl.create({
message: text,
duration: duration || closeButton ? null : 3000,
position: position || 'top',
showCloseButton: closeButton || false,
closeButtonText: btnText || 'OK'
});
this._toastMsg.present();
},
hide() {
this._toastMsg.dismiss();
}
}
现在显示 toast 像this.shared.Toast.show('message');
。同样你可以在这里添加Storage、Loader等常用功能。