[英]How to resolve "property doesn't exists in type object" error in angular?
在我的应用程序中有一个父组件,其中包含代表不同水果的对象列表。 当用户选择水果时,其数据将传递给子组件,子组件将所有数据显示为详细信息。
<app-details
[fruit]="selectedFruit"
></app-details>
在详细信息模板中:
<div class="fruit-details">
<h1>{{fruit.name}}</h1>
<h1>{{fruit.color}}</h1>
<h1>{{fruit.description}}</h1>
</div>
'fruit' 属性在详细信息组件中设置为类型 object。 @Input()fruit: Object;
发生错误,指出Property 'description' does not exist on type 'Object'
。 不将“水果”属性设置为“对象”的数据类型可以解决问题。 但是如何在不删除数据类型的情况下解决这个问题。
为Fruit
声明一个接口,然后将其用作您的类型:
interface Fruit {
name: string;
color: string;
description: string;
}
@Input()fruit: Fruit;
如何使用 @Input()fruit: string,将您选择的水果字符串化
JSON.stringify(selectedFruit)
详细组件解析它:
let parseFruit = JSON.parse(this.fruit);
console.log(parseFruit);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.