如何在角度循环中使用日期管道?

分享于2022年07月17日 angular date-pipe dictionary javascript typescript 问答
【问题标题】:如何在角度循环中使用日期管道?(How to use date-pipe inside for loop in angular?)
【发布时间】:2022-01-26 11:24:17
【问题描述】:

我想对我的字典的值使用日期管道。所以我尝试了这个。

  • {{field.key}}: {{field.value | date:'d MMM y'}}
  • 但出现错误

        Error: src/app/update-watchlist/update-watchlist.component.html:45:76 - error TS2769: No overload matches this call.
      Overload 1 of 3, '(value: string | number | Date, format?: string | undefined, timezone?: string | undefined, locale?: string | undefined):
    string | null', gave the following error.
        Argument of type 'unknown' is not assignable to parameter of type 'string | number | Date'.
          Type 'unknown' is not assignable to type 'Date'.
      Overload 2 of 3, '(value: null | undefined, format?: string | undefined, timezone?: string | undefined, locale?: string | undefined): null', gave the following error.
        Argument of type 'unknown' is not assignable to parameter of type 'null | undefined'.
          Type 'unknown' is not assignable to type 'null'.
      Overload 3 of 3, '(value: string | number | Date | null | undefined, format?: string | undefined, timezone?: string | undefined, locale?: string | undefined): string | null', gave the following error.
        Argument of type 'unknown' is not assignable to parameter of type 'string | number | Date | null | undefined'.
          Type 'unknown' is not assignable to type 'Date'.
    
    45                     
  • {{field.key}}: {{field.value | date:'d MMM y'}}
    • watchlist 的类型是什么?
    • 这是一本字典
    • 在打字稿中 Dictionary ?还是 object

    【解决方案1】:

    确保 field.value string number Date 。目前被视为 unknown

    为此,您必须更好地输入 watchlist 或明确地输入 field.value