angular - 内部方法完成后如何关闭 LoadingController?
问题描述
我正在创建一个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())
));
}
解决方案
有两种方法可以做到这一点。
- 方法完成后,您可以发出事件。
- 您可以从对象返回由 .find() 方法创建的 Promise。在您的组件代码中,您可以使用 async/await 或 .then() 来调用加载控制器的 .dismiss() 。
我建议使用方法#2,因为那是“正常的”。一旦你把服务的方法变成了一个承诺,你可以如下调整你的代码:
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()
});
- 注意:这还不完整,所以我无法测试语法错误。希望你明白这一点。
推荐阅读
- azure - 对 azure 免费层中的出站数据传输感到困惑
- c# - 从带有过滤器的目录中获取所有文件,不包括特定过滤器
- apache-kafka - Kafka Sink: ERROR 由于错误而停止 (org.apache.kafka.connect.cli.ConnectStandalone:130)
- java - 字符串长度;(vs) str.length()
- sql - 访问字段列表中的嵌套选择
- c# - 使放置在同一位置的组框的可见性消失 - C#
- reactjs - 将 React 变量传递给“href”属性
- javascript - 使用相同的连接 ID 发出信号 r 客户端重新连接
- docker-compose - 检查docker容器中的值后如何从docker-compose应用配置(安装卷)
- r - 使用 ggplotly 时定义滑块的平移