[英]Property does not exist on object - Angular
I have an object, which I want to fill and use the description
property我有一个 object,我想填写并使用
description
属性
public obj = {};
setObj(){
this.sp.getResourceUrl(this.results[0].resource_url).subscribe(data => {
this.obj = data; // data has description property
});
}
It worked fine for a while with the following html code but somehow the error "The property description does not exists on type {}"使用以下 html 代码可以正常工作一段时间,但不知何故出现错误“类型 {} 上不存在属性描述”
<div *ngIf="obj && obj.description != ''; else notfound">
{{obj.description}}
</div>
<ng-template #notfound>
<div>
Description not found.
</div>
</ng-template>
Do I have to set all properties I receive from the object I get via GET-Request?我是否必须设置从通过 GET-Request 获得的 object 收到的所有属性? If yes, why?
如果是,为什么? It worked fine before.
它以前工作得很好。
This is a typescript warning to you but a fine javascript code.这是对您的 typescript 警告,但是是一个很好的 javascript 代码。
Yes, you should ideally define type of properties you are using in your classes, this way it helps to mitigate unseen errors which can happen during runtime.是的,理想情况下,您应该定义您在类中使用的属性类型,这样有助于减轻运行时可能发生的不可见错误。 For example if you are accessing
{{obj.description}}
instead of {{obj.descriptions}}
, typescript warns you before hand rather than you finding it at runtime that there is no description
on obj
例如,如果您访问的是
{{obj.description}}
而不是{{obj.descriptions}}
,typescript 会事先警告您,而不是在运行时发现obj
没有description
You can have an Interface or a type for these props.这些道具可以有一个接口或一个类型。
Here for example, it should be:例如,这里应该是:
public obj: {descriptions?: string} = {}; // add more keys if you get more keys hetre
One way to bypass your errors would be to type it as any
绕过错误的一种方法是将其键入为
any
public obj: any = {};
You are just checking for obj
availability and not its description
.您只是在检查
obj
的可用性,而不是它的description
。
As this line public obj = {};
作为这一行
public obj = {};
says, obj
is just an empty object in the first and obj.description
is undefined
.说,
obj
只是第一个空的 object 并且obj.description
是undefined
。
So you should first check for the description
availability and then check the value or etc.因此,您应该首先检查
description
的可用性,然后检查值等。
Please use the following code请使用以下代码
<div *ngIf="obj && obj.description; else notfound">
{{obj.description}}
</div>
<ng-template #notfound>
<div>
Description not found.
</div>
</ng-template>
ie it will display if and only if obj
and obj.description
will exist otherwise notfound
will be displayed即当且仅当
obj
和obj.description
存在时才会显示,否则将显示notfound
The solution is check for null or undefined (to see whether the object exists) using safe navigation operator解决方案是使用安全导航运算符检查 null 或未定义(查看 object 是否存在)
<div *ngIf="obj && obj?.description != ''; else notfound">
Also make sure you are defining the type if you already know it, or use any如果您已经知道类型,请确保您正在定义类型,或者使用任何类型
this.sp.getResourceUrl(this.results[0].resource_url).subscribe((data : any) => {
this.obj = data; // data has description property
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.