[英]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())
));
}
有两种方法可以做到这一点。
我建议使用方法#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.