如何在 angular 中的 for 循环中使用日期管道?

How to use date-pipe inside for loop in angular?

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

<div *ngFor="let field of watchlist | keyvalue">
<li *ngIf="field.value  && field.key">{{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                     <li *ngIf="field.value  && field.key">{{field.key}}: {{field.value | date:'d MMM y'}}

确保 field.valuestringnumberDate。目前显示为 unknown.

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