另一个组件 Ionic 4 内部的组件
Component inside of another Component Ionic 4
我正在将我的应用程序从 Ionic 3
迁移到 Ionic 4
,我发现每个组件都在使用 async and await calls
。我明白为什么要使用它,但在我的 Ionic 3 应用程序中,我在 Alert Controller Component
.
中有一个嵌套的 Loading 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();
}
我正在将我的应用程序从 Ionic 3
迁移到 Ionic 4
,我发现每个组件都在使用 async and await calls
。我明白为什么要使用它,但在我的 Ionic 3 应用程序中,我在 Alert Controller Component
.
Loading 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();
}