定义方法
import { ToastController, AlertController } from '@ionic/angular';
constructor(
public router: Router,
public http: InterceptorService,
public tipMessageService: TipMessageService,
public toastController: ToastController,
public alertController: AlertController,
) {
super();
}
async presentAlertNoData(
alertController: AlertController,
message: string,
router?: Router,
cancelUrl?: string,
) {
const alert = await alertController.create({
header: '^_^温馨提示',
message: message,
backdropDismiss: false,
mode: 'ios',
buttons: [
{
text: '取消',
role: 'cancel',
cssClass: 'secondary',
handler: () => {
if (cancelUrl !== null && cancelUrl !== undefined) {
router.navigateByUrl(cancelUrl);
} else {
console.log('Cancel Okay');
}
}
}, {
text: '确定',
handler: () => {
this.selectTotalNum();
}
}
]
});
await alert.present();
}
async noDataToast() {
const toast = await this.ToastController.create({
message: '查询失败!!',
duration: 1000,
position: 'middle',
cssClass: 'hometosat'
});
toast.present();
}
方法调用:
selectTotalNumber() {
this.recordNum = window.localStorage.getItem('studyNumber');
this.userId = window.localStorage.getItem('userId');
const url = 'english-web/word/queryWordData/' + this.recordNum + '/' + this.userId ;
this.http.get(url).subscribe(
res => {
if (res.json().code === ResponseCode.SUCCESSCODE) {
if (res.json().data.length === 0) {
this.presentAlertNoData(this.alertController, '优秀的你已经把所有单词都学完了,是否再学习一遍?', this.router, 'home-main');
} else {
this.pictureList = res.json().data;
this.picture1 = this.pictureList[0].wordPicture1;
this.picture2 = this.pictureList[0].wordPicture2;
this.picture3 = this.pictureList[0].wordPicture3;
this.picture4 = this.pictureList[0].wordPicture4;
this.audio = this.pictureList[0].audio;
}
} else if (res.json().code === ResponseCode.FAILCODE) {
this.noDataToast();
}
}
);
}