简体   繁体   English

内部方法完成后如何关闭 LoadingController?

[英]How to dismiss LoadingController once a method inside is complete?

I am creating a LoadingController below, presenting it, & executing some methods:我在下面创建一个LoadingController ,展示它,并执行一些方法:

this.loadingCtrl.create({
      message: 'Sending Message...'
    }).then(loadingEl => {
      loadingEl.present();
      this.conversationsService.addMessageToConversation(this.conversation.id, this.form.value.message);
      this.loadMsg();
      this.form.reset();
    });

Once addMessageToConversation() is successfuly, I then want to dismiss the LoadingController.一旦addMessageToConversation()成功,我就想dismiss LoadingController。

Can someone please tell me how I can do this?有人可以告诉我我该怎么做吗?

Here is addMessageToConversation() in case it is required:如果需要,这里是addMessageToConversation()

addMessageToConversation(conversationId: string, message: string) {
    this._conversations.getValue().find(conversation => conversation.id === conversationId)
      .messages.push(
        new Message(
          Math.random().toString(),
          message,
          this.authService.userId,
          new Date(Date.now())
        ));
  }

There are two ways to do this.有两种方法可以做到这一点。

  1. You can emit an event once your method is completed.方法完成后,您可以发出事件。
  2. You can return the Promise created by.find() method from your object.您可以从 object 中返回由.find() 方法创建的 Promise。 From there in your component code you can use async/await or.then() to call the.dismiss() of your loading controller.在您的组件代码中,您可以使用 async/await 或 .then() 调用加载 controller 的 the.dismiss()。

I'd suggest using Method #2 as that is "normal".我建议使用方法#2,因为那是“正常的”。 Once your turn the service's method into a promise you can adjust your code as follows:将服务的方法转换为 promise 后,您可以按如下方式调整代码:

 this.loadingCtrl.create({ message: 'Sending Message...' }).then(async loadingEl => { loadingEl.present(); const res = await this.conversationsService.addMessageToConversation(this.conversation.id, this.form.value.message); this.loadMsg(); this.form.reset(); this.loadingCtrl.dismiss() });

  • NOTE: this is not complete, so I was not able to test for syntax errors.注意:这还不完整,所以我无法测试语法错误。 Hopefully you get the point.希望你明白这一点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM