如何解决有没有办法保证用户等待操作
因此,我在Angular中创建了一个确认模态,该确认的工作方式如下:
1.-我有一个组件(Alert.components.ts),该组件处理模板并订阅Alert.service:
export class AppAlertComponent implements OnInit {
constructor(private alertService: AlertServiceDsm) {}
ngOnInit() {
this.alertService.onAlert()
.subscribe(alert => {
// this fires the confirmation modal
this.initModal(alert);
});
}
在模板中,当用户单击“是”时,我有一个事件绑定
<button (click)="onConfirm()"> Yes </button>
2.-我有一项服务(Alert.service.ts),可以在任何组件中请求警报时更新主题:
fire(text: string) {
this.subject.next(text);
}
3.-当任何组件需要触发警报时,它将调用Alert.service.ts中的方法,正如我已经告诉您的那样,这将更新主题
// in any other component
this.alertService.fire('Are you sure?');
所以我希望能够做这样的事情:
this.alertService.fire('Are you sure?')
.then(response => {
if (response) {
// handle confirm logic here...
} else {
// close the alert
this.alertService.remove();
}
})
但是,我陷入了困境,无法在服务,alert.component和触发警报的组件之间划分承诺。如果有人可以帮助我,我将非常感谢,并在此先感谢。
解决方法
您是否尝试创建一个额外的变量,例如标志变量(可能是一个Subject),并且当用户执行某项操作并从需要侦听该操作的另一个组件进行订阅时,将值推入subject?
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。