【问题标题】:Angular error TS2339 Property does not exist on type角度错误 TS2339 属性在类型上不存在
【发布时间】:2020-08-04 09:19:57
【问题描述】:

大家好,最近我开始学习一些 Angular。

目前我收到以下错误:

32  this.warning.error.push(entry.name+': '+entry.error);
                 ~~~~~
src/app/dashboard/dashboard.component.ts:33:15 - error TS2339: Property 'id' does not exist on type 'Warning[]'.

33  this.warning.id.push(entry.id);
                 ~~
src/app/dashboard/dashboard.component.ts:37:13 - error TS2339: Property 'error' does not exist on type 'Error[]'.

37  this.error.error.push(entry.name+': '+entry.error);
               ~~~~~
src/app/dashboard/dashboard.component.ts:38:13 - error TS2339: Property 'id' does not exist on type 'Error[]'.

38  this.error.id.push(entry.id);
               ~~

问题是我为这两个 importet 定义了接口。

export interface Error {
        id: number;
        error: string;
        }

export interface Warning {
        id: number;
        error: string;
        }

正如你在我的组件中看到的那样。

import { Error, Warning } from '../dashboard';
...
export class DashboardComponent implements OnInit {
 error: Error[];
 warning: Warning[];
...

evaluate(): void{
        for (let entry of this.status){
        if (entry.status === 0){
                this.ok = this.ok + 1;
                }
        if (entry.status === 1 && entry.value < 8){
        this.warnings = this.warnings + 1;
        this.warning.error.push(entry.name+': '+entry.error);
        this.warning.id.push(entry.wt_id);
                }
        if (entry.status === 1 && entry.value >= 8){
        this.critical = this.critical + 1;
        this.error.error.push(entry.wt_name+': '+entry.error);
        this.error.id.push(entry.wt_id);
                }
        }
}

我已经尝试了一些我在旧帖子中找到的东西,但似乎没有任何效果。

也许你们中的一些人知道解决方法并可以指出来。也许只是我错过了一些东西。

干杯!

【问题讨论】:

    标签: angular typescript


    【解决方案1】:

    您不是推送到成员变量errorwarning,而是推送到变量中的属性。

    1. 您需要将类型定义更改为数组

    接口

    export interface Error {
      id: number[];
      error: string[];
    }
    
    export interface Warning {
      id: number[];
      error: string[];
    }
    

    组件

    export class DashboardComponent implements OnInit {
      error: Error;
      warning: Warning;
     
      ...
    }
    
    1. 或者改为推送到成员变量。作为@GunnerB。在 cmets 中指出,数组 errorwarning 在尝试使用 push 函数向其推送值之前也需要初始化。

    接口

    export interface Error {
      id: number;
      error: string;
    }
    
    export interface Warning {
      id: number;
      error: string;
    }
    

    组件

    export class DashboardComponent implements OnInit {
      error: Error[] = [];              // <-- initialize the arrays
      warning: Warning[] = [];
      ...
    
      evaluate(): void {
        for (let entry of this.status) {
          if (entry.status === 0) {
            this.ok = this.ok + 1;
          }
          if (entry.status === 1 && entry.value < 8) {
            this.warnings = this.warnings + 1;
            this.warning.push({id: entry.wt_id, error: entry.name + ': ' + entry.error });
          }
          if (entry.status === 1 && entry.value >= 8) {
            this.critical = this.critical + 1;
            this.error.push({id: entry.wt_id, error: entry.name + ': ' + entry.error });
          }
        }
      }
    }
    

    【讨论】:

    • 打败我。不过还有一个补充:两个数组需要在某处初始化(至少在 OPs 代码中没有显示)。向两者添加= [] 就足够了。
    • @GunnarB.:是的,这是一个经常被忽视的问题。我已经编辑了答案。谢谢。
    • 不错的@Michael D 真的很好,而且似乎有效;)
    【解决方案2】:

    请改为: `this.warning.error = entry.wt_name + ': ' + entry.error;

    Warning 接口中的error 是字符串而不是数组。

    【讨论】:

    • 但我需要它是一个数组。所以我想我应该把它改成error: Error[] = [];? @Raz Ronen
    • 正确。并且有:export interface Warning { id: number[]; error: string[]; }
    • 嗯很好。很高兴知道多种方式。感谢您的参与!
    猜你喜欢
    • 2020-07-07
    • 1970-01-01
    • 1970-01-01
    • 2019-01-21
    • 2016-08-13
    • 2017-12-20
    • 1970-01-01
    • 2016-11-14
    • 2017-10-24
    相关资源
    最近更新 更多