angular - 如何修复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);
}
}
}
解决方案
这个错误是警告你在 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
}
}
}
推荐阅读
- android - 使用 Glide 库构建 gradle 失败后,我的清单丢失了
- java - Java - 迭代 arrayList 并将 250k 单词插入到 treeMap 需要大量时间
- python - 在 matplotlib 中,我可以将标签设置在绘图的底部但刻度线为零吗?
- sql-server - 在 ASP.NET MVC 中,只有特定数据的首字母才会保存在数据库中
- javascript - 无法使用 highlight.js 以正确的顺序呈现 XML
- sql-server - 在存储过程 SQL Server 中的 Select 查询中执行动态查询
- node.js - 与私有子网中的 EC2 Node.js 服务器的连接超时
- android - Android项目:gradle配置失败
- c++ - 将基实例指针转换为派生实例指针是否合法?(实例不是派生实例)
- java - 使用 mySQL 的 Java Hibernate 应用程序 - 登录页面不起作用