简体   繁体   English

object - Angular 上不存在属性

[英]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.descriptionundefined

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即当且仅当objobj.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.

相关问题 类型错误时不存在 Angular 属性 - Angular Property does not exist on type error Angular 13 属性在类型“FormGroup”上不存在 - Angular 13 Property does not exist on type 'FormGroup' ngFor 属性“id”在“对象”类型上不存在 - ngFor Property 'id' does not exist on type 'Object' “对象”类型上不存在属性“边框” - Property 'bordered' does not exist on type 'Object' 与角度打字稿一起使用时,类型“GlobalEventHandlers”上不存在属性 classList - property classList does not exist on type 'GlobalEventHandlers' when used with angular typescript Angular ngIf 中的类型“y”上不存在属性“x” - Property 'x' does not exist on type 'y' in Angular ngIf Textarea 的“HTMLElement”类型上不存在属性“value”-Angular - Property 'value' does not exist on type 'HTMLElement' for Textarea - Angular 类型“对象”上不存在属性“过滤器”。 尝试过滤响应时 - Property 'filter' does not exist on type 'Object'. When trying to filter response 属性“ariaHidden”不存在 - Property 'ariaHidden' does not exist 尝试将属性绑定到 ng model 时,如何修复 Angular 错误“用户”类型上不存在属性“银行代码”? - How to fix Angular error Property 'bankCode' does not exist on type 'User' when trying to bind the property to ng model?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM