如何修复primeNG日历中的“未捕获(承诺)”错误

时间:2019-05-09 10:26:10

标签: angular primeng

我正在设置primeNG日历,想知道如何在编辑时显示保存的日期。enter image description here

<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); 
        }
    }
} 

1 个答案:

答案 0 :(得分:0)

此错误警告您承诺中发生了错误,但是没有处理程序。

由于您将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
        }
    }
}