【问题标题】:PrimeNG ConfirmationService does not wait for an answerPrimeNG ConfirmationService 不等待答复
【发布时间】:2020-09-25 12:44:26
【问题描述】:

我有一个具有以下功能的父组件:

@ViewChild('currentTab') currentTab: ChildComponent;  
  
nextTab(): void {
  if (this.currentTab.save()) {
    this.activeIndex++; 
  }
}

子组件上的方法如下:

save(): boolean {
  return this.confirmationService.confirm({
    message: 'Are you sure?',
    accept: () => true,
    reject: () => false
  });
}

问题:父组件中的条件不等待答案,它得到“真”,执行this.activeIndex++;。我做错了什么?

提前致谢。

【问题讨论】:

    标签: angular promise observable primeng


    【解决方案1】:

    Javascript 始终是同步单线程。所以表达式this.currentTab.save() 被解析并立即评估if(....) 语句。根据PrimeNg source code confirm(...) 方法触发一个信号以显示弹出对话框然后返回this 这是一个真实值。所以预计activeIndex总是增加。

    为了实现你想要的,在父组件中导入并使用确认服务:

    nextTab(): void {
      this.confirmationService.confirm({
         message: 'Are you sure?',
         accept: () => this.activeIndex++,
         reject: () => //Do something else
      });
    }
    

    【讨论】:

    • 我也试过了,这也不起作用,也不返回任何东西
    • 是的,关键是this.confirmationService.confirm 方法作为处理程序工作,而不是返回任何值
    • 这里是这个问题的答案stackoverflow.com/questions/65239651/…
    • 那么它是作为处理程序工作而不是返回值吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-15
    • 2021-02-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-26
    相关资源
    最近更新 更多