为长度为“0”的元组类型“[]”构建角度项目时出错,索引“0”处没有元素

分享于2023年05月08日 angular javascript typescript 问答
【问题标题】:Error while building an angular project for Tuple type '[]' of length '0' has no element at index '0'为长度为“0”的元组类型“[]”构建角度项目时出错,索引“0”处没有元素
【发布时间】:2023-05-07 12:45:02
【问题描述】:

以下是我执行以下prod命令时的错误

ng build --prod

src/app/inventory/inv-parts-main-table/dialog-component/dialog-component.component.html(5,16) 中的错误:长度为 '0' 的元组类型 '[]' 没有元素在索引“0”处。

src/app/inventory/inv-parts-main-table/dialog-component/dialog-component.component.html(5,62):长度为“0”的元组类型“[]”在索引处没有元素'0'。

src/app/inventory/inv-parts-main-table/dialog-component/dialog-component.component.html(5,62):长度为“0”的元组类型“[]”在索引处没有元素'0'。

src/app/inventory/inv-parts-main-table/dialog-component/dialog-component.component.html(9,16):长度为“0”的元组类型“[]”在索引处没有元素'1'。

src/app/inventory/inv-parts-main-table/dialog-component/dialog-component.component.html(9,62):长度为“0”的元组类型“[]”在索引处没有元素'1'。

src/app/inventory/inv-parts-main-table/dialog-component/dialog-component.component.html(9,62):长度为“0”的元组类型“[]”在索引处没有元素'1'。

src/app/inventory/inv-parts-main-table/dialog-component/dialog-component.component.html(13,16):长度为“0”的元组类型“[]”在索引处没有元素'2'。

src/app/inventory/inv-parts-main-table/dialog-component/dialog-component.component.html(13,62):长度为“0”的元组类型“[]”在索引处没有元素'2'。

src/app/inventory/inv-parts-main-table/dialog-component/dialog-component.component.html(13,62):长度为“0”的元组类型“[]”在索引处没有元素'2'。

上述错误相关的源码如下所示

{{data.quote[0] | currency}}
{{data.lastUpdatedDate[0] | date :'mediumDate'}} | {{data.supplierName[0]}}
{{data.quote[1] | currency}}
{{data.lastUpdatedDate[1] | date :'mediumDate'}} | {{data.supplierName[1]}}
{{data.quote[2] | currency}}
{{data.lastUpdatedDate[2] | date :'mediumDate'}} | {{data.supplierName[2]}}

Ts 文件如下所示

export interface DialogData {
  quote: [];
  lastUpdatedDate : [];
  supplierName: [];
}


【解决方案1】:

以下 ts 更改帮助我解决了这个问题

长度为“0”的元组类型“[]”在索引“1”处没有元素。

export interface DialogData {
  quote: string[];
  lastUpdatedDate : string[];
  supplierName: string[];
}

【讨论】: