我将我的应用程序从Ionic 3迁移到Ionic 3,我看到每个组件都在使用async and await calls。我理解为什么要使用它,但是在我的Ionic 3应用程序中,我在Alert Controller Component中有一个嵌套的Alert Controller Component。
离子3 .ts
submitAssetLoc(form: NgForm){
const alert = this.alertCtrl.create({
header: 'Submit',
message: 'Are you sure you want to Submit?',
buttons: [
{
text: 'Yes',
role: 'Yes',
handler: () => {
const loading = this.loadingCtrl.create({
message: 'Submitting...'
});
loading.present();
this.stemAPI.updateGPSLoc(this.testData).subscribe((result) => {
loading.dismiss();
}, (err) => {
loading.dismiss();
let alert = this.alertCtrl.create({
header: 'Error: Could not submit!',
message: 'Try submitting again, or submit offline!',
buttons: [
{
text: 'Try Submitting Again',
role: 'Yes',
handler: () => {
this.newGPSLoc = [];
}
},
{
text: 'Submit Offline',
handler: () => {
this.navCtrl.push(SuccessPage, { 'APIresponse': 'Form submitted offline, please go to support page and re-submit!'});
}
}
]
});
alert.present();
}
)}
},
{
text: 'No',
role: 'Cancel',
handler: () => {
}
}
]
});
alert.present();
}
我的问题是异步,等待没有正确实现的调用,我知道--我的代码--显然不是高效的。我想我需要为其中的每一个创建方法,但是正确实现这个特性的最佳方法是什么呢?
发布于 2019-05-07 17:12:37
希望这能帮上忙
async submitAssetLoc(form: NgForm){
const alert = await this.alertCtrl.create({
header: 'Submit',
message: 'Are you sure you want to Submit?',
buttons: [
{
text: 'Yes',
role: 'Yes',
handler: async () => {
const loading = await this.loadingCtrl.create({
message: 'Submitting...'
});
loading.present();
this.stemAPI.updateGPSLoc(this.testData).subscribe((result) => {
loading.dismiss();
}, async (err) => {
loading.dismiss();
let alert = await this.alertCtrl.create({
header: 'Error: Could not submit!',
message: 'Try submitting again, or submit offline!',
buttons: [
{
text: 'Try Submitting Again',
role: 'Yes',
handler: () => {
this.newGPSLoc = [];
}
},
{
text: 'Submit Offline',
handler: () => {
this.navCtrl.push(SuccessPage, { 'APIresponse': 'Form submitted offline, please go to support page and re-submit!'});
}
}
]
});
alert.present();
}
)}
},
{
text: 'No',
role: 'Cancel',
handler: () => {
}
}
]
});
alert.present();
}https://stackoverflow.com/questions/56026962
复制相似问题