首页 > 解决方案 > 如何修复primeNG日历中的“未捕获(承诺)”错误

问题描述

我正在设置 primeNG 日历,并想知道如何在编辑时显示保存的日期。在此处输入图像描述

<p-calendar name="startDate" [(ngModel)]="classPrg.startDate"> </p-calendar> 
<p-calendar name="endDate" [(ngModel)]="classPrg.endDate"></p-calendar>
ngOnInit() { 
    this.classPrg = this.classPrgStore.state.entry.data; 
    if (!this.classPrg) { 
        let id: string = this.activedRoute.snapshot.params['id']; 
        if (await this.classPrgStore.loadClassPrg(id).toPromise()) { 
            this.classPrg = this.classPrgStore.state.entry.data; 
            this.classPrg.startDate = new Date(this.classPrg.startDate); 
            this.classPrg.endDate = new Date(this.classPrg.endDate); 
        }
    }
} 

标签: angularprimeng

解决方案


这个错误是警告你在 Promise 中发生了错误但没有处理程序。

由于您正在使用async/await您的承诺,因此您需要一个catch块:

ngOnInit() { 
    this.classPrg = this.classPrgStore.state.entry.data; 
    if (!this.classPrg) { 
        let id: string = this.activedRoute.snapshot.params['id']; 
        try {
            if (await this.classPrgStore.loadClassPrg(id).toPromise()) { 
                this.classPrg = this.classPrgStore.state.entry.data; 
                this.classPrg.startDate = new Date(this.classPrg.startDate); 
                this.classPrg.endDate = new Date(this.classPrg.endDate); 
            }
        } catch (error) {
            // Handle error, or just leave blank to ignore error
        }
    }
} 

推荐阅读