我正在嘗試顯示以下物件:
export const articles = {
hardware: ['CPU', 'Motherboard', 'GPU', 'RAM', 'Power Supply'],
Software: ['Operating System', 'Program', 'Web Site'],
compsci: ['Algorithm', 'Programming', 'Data Structure']
}
像這樣:
<h2>Browse through our articles:</h2>
<section *ngFor="let key of keys">
{{key}}
<div *ngIf="myArticles.hasOwnProperty(key)">
<span *ngFor="let arr of myArticles[key]"><app-list-detail [list]="arr"></app-list-detail></span>
</div>
</section>
當ARR被發送到一個子組件的串列。但我得到標題中的錯誤。我認為是因為 TS 無法知道key是否是myArticles的實際屬性,所以我在上面添加了 * ngIf。
但它仍然不起作用,我不明白為什么。
uj5u.com熱心網友回復:
您可以使用keyvalue管道迭代物件:
<h2>Browse through our articles:</h2>
<section *ngFor="let entry of articles | keyvalue">
{{entry.key}}
<span *ngFor="let arr of entry.value"><app-list-detail [list]="arr"></app-list-detail></span>
</section>
uj5u.com熱心網友回復:
要克服該錯誤,您需要為articles. 您可以將型別指定為:
export const articles: Record<string, string[]> = {
hardware: ['CPU', 'Motherboard', 'GPU', 'RAM', 'Power Supply'],
Software: ['Operating System', 'Program', 'Web Site'],
compsci: ['Algorithm', 'Programming', 'Data Structure']
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/407282.html
標籤:
上一篇:為什么L擴展AUnion,R擴展AUnion沒有重疊
下一篇:清除物件內每個鍵的每個值
