【问题标题】:How to use date-pipe inside for loop in angular?如何在角度循环中使用日期管道?
【发布时间】:2022-01-26 11:24:17
【问题描述】:

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

<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'}}

【问题讨论】:

  • watchlist的类型是什么?
  • 这是一本字典
  • 在打字稿中Dictionary?还是object

标签: javascript angular typescript dictionary date-pipe


【解决方案1】:

确保field.valuestringnumberDate。目前被视为unknown

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-18
    • 1970-01-01
    相关资源
    最近更新 更多