微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Component.html:151 错误错误:InvalidPipeArgument:“无法将“14-05-2021”转换为管道“DatePipe”的日期

如何解决Component.html:151 错误错误:InvalidPipeArgument:“无法将“14-05-2021”转换为管道“DatePipe”的日期

我收到后端 LastDate回复。我将此绑定到我的 ngModel显示在网格中,但在控制台中我收到此错误

enter image description here

这里是对应的代码

<ngx-datatable-column name="Last Date" prop="LastDate">
  <ng-template let-row="row" ngx-datatable-cell-template>
    {{row.LastDate| date: 'dd-MM-yyyy'}}
  </ng-template>
</ngx-datatable-column>

解决方法

日期 (String) "14-05-2021" 会被 Javascript 错误地解释。它接受格式为“MM-DD-YYYY”的日期,而您的输入格式为“DD-MM-YYYY”。快速解决方法是在将字符串发送到 date 管道之前重新格式化字符串。

控制器 (*.ts)

backendFetch().subscribe({
  next: (res: any) => {
    const s = res.LastDate.split("-");
    this.row = {
      ...res,LastDate: `${s[1]}-${s[0]}-${s[2]}`;
    }
  },error: (error: any) => { }
);

通过这种方式,LastDate 对象的 row 属性被调整为预期的格式“MM-DD-YYYY”。

显然,使用 Array#split 的转换是微不足道的。我相信其他人可以想出更好的解决方案(例如使用 RegEx)。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。