首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >另一个组件Ionic 4的内部组件

另一个组件Ionic 4的内部组件
EN

Stack Overflow用户
提问于 2019-05-07 16:34:12
回答 1查看 685关注 0票数 0

我将我的应用程序从Ionic 3迁移到Ionic 3,我看到每个组件都在使用async and await calls。我理解为什么要使用它,但是在我的Ionic 3应用程序中,我在Alert Controller Component中有一个嵌套的Alert Controller Component

离子3 .ts

代码语言:javascript
复制
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();
  }

我的问题是异步,等待没有正确实现的调用,我知道--我的代码--显然不是高效的。我想我需要为其中的每一个创建方法,但是正确实现这个特性的最佳方法是什么呢?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-05-07 17:12:37

希望这能帮上忙

代码语言:javascript
复制
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();
  }
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56026962

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档