簡體   English   中英

內部方法完成后如何關閉 LoadingController?

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

我在下面創建一個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();
    });

一旦addMessageToConversation()成功,我就想dismiss LoadingController。

有人可以告訴我我該怎么做嗎?

如果需要,這里是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())
        ));
  }

有兩種方法可以做到這一點。

  1. 方法完成后,您可以發出事件。
  2. 您可以從 object 中返回由.find() 方法創建的 Promise。 在您的組件代碼中,您可以使用 async/await 或 .then() 調用加載 controller 的 the.dismiss()。

我建議使用方法#2,因為那是“正常的”。 將服務的方法轉換為 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() });

  • 注意:這還不完整,所以我無法測試語法錯誤。 希望你明白這一點。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM