角错误TS2339属性在类型上不存在

时间:2020-08-04 09:19:57

标签: angular typescript

大家好,我最近开始学习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);
               ~~

问题是我为两个重要的接口定义了接口。

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);
                }
        }
}

我已经尝试过一些在较早的帖子中发现的东西,但似乎没有任何作用。

也许你们中的一些人知道一个修复程序,可以指出来。也许只是我想念的东西。

干杯!

2 个答案:

答案 0 :(得分: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。注释中指出,在尝试使用error函数将值推入数组之前,还需要初始化数组warningpush

接口

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 });
      }
    }
  }
}

答案 1 :(得分:0)

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

error接口中的

Warning是字符串而不是数组。