angular - 另一个组件 Ionic 4 中的组件
问题描述
我正在将我的应用程序迁移到Ionic 4
from Ionic 3
,我看到async and await calls
每个组件都在使用它。我理解为什么要使用它,但是在我的 Ionic 3 应用程序中,我有一个嵌套Loading 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();
}
我的问题是 async,await 调用没有正确实现,我知道我的代码显然效率不高。我假设我需要为每一个都制定方法,但是正确实现此功能的最佳方法是什么?
解决方案
希望这会有所帮助
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();
}
推荐阅读
- ruby-on-rails - 您必须在发出请求之前设置 ShopifyAPI::Base.api_version
- bash - Bash getopt 接受多个参数
- sql - 我想在列上添加一个唯一约束。在运行升级之前,请检查该列是否有重复值
- apache-spark - 我正在使用本地机器上的 Spark-Scala。火花版本 3.0.1
- android - 允许访问单个 URL Android Chrome
- reactjs - 加载不应具有基本名称的新页面时,如何在 React Link 组件中清除基本名称?
- amazon-quicksight - 计数一目了然
- integromat - Integromat 应用程序 {{connection.param}} 不工作
- python - “utf-8”编解码器无法解码字节 0xe1:数据意外结束
- vb.net - VB.NET - DAL - “GetProductsByCategoryID”不是“ProductsTableAdapter”的成员,但类视图显示它是