![](/img/trans.png)
[英]Element implicitly has an 'any' type because expression of type '"logo"' can't be used to index type 'Object' in Angular
[英]'Element implicitly has an 'any' type because expression of type 'string' can't be used to index type in Object' in *ngFor
我正在尝试显示以下 object:
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作为list发送到子组件的位置。 但我得到标题中的错误。 我认为是因为 TS 无法知道key是否是myArticles的实际属性,所以我在上面添加了 * ngIf 。
但它仍然不起作用,我不明白为什么。
您可以使用键值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>
要克服该错误,您需要指定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']
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.