我有一个包含15个属性的TypeScript类。其中14个由WebAPI调用的响应填充。另一个意思是稍后设定。
出于某种原因,我收到以下错误:
> ./ClientApp/app/components/home/IncidentTable.component.ts中的错误 (45,18):错误TS2339:属性'CurrentStatusCssClasses'没有 存在于'IncidentNode'类型
这是我的班级:
export class IncidentNode {
public CurrentStatusCssClasses: string[];
public RetrievalId: number;
public IncidentId: number;
public Name: string;
public IPAddress: string;
public MonitorStartTime: string;
public LastStatusTime: string;
public IssueTime: string;
public IssueStatus: string;
public LastAnalysisTime: string;
public CurrentStatus: string;
public LastProblemTime: string;
public ProblemCount: number;
}
以下是组件(错误发生在node.CurrentStatusCssClasses.push()
行上):
import { Component, Input, OnInit } from '@angular/core';
import { Observable, Subscription, Subject } from 'rxjs';
import * as moment from 'moment';
import { IncidentNode } from './IncidentNode';
@Component({
selector: 'incident-node-table',
template: require('./IncidentTable.component.html'),
styles: [require('./IncidentTable.component.css')]
})
export class IncidentNodeTableComponent implements OnInit {
@Input() parentSubject: Subject<any>;
@Input() tableTitle: string;
IncidentNodes: IncidentNode[] = null;
ngOnInit() {
this.parentSubject.subscribe(event => {
if (event != null) {
this.IncidentNodes = event;
this.formatFields();
}
});
}
formatFields() {
for (let i = 0; i < this.IncidentNodes.length; i++) {
let node = this.IncidentNodes[i];
if (node.IssueTime != null) {
node.IssueTime = this.formatDateInternal(node.IssueTime);
}
if (node.LastAnalysisTime != null) {
node.LastAnalysisTime = this.formatDateInternal(node.LastAnalysisTime);
}
if (node.LastProblemTime != null) {
node.LastProblemTime = this.formatDateInternal(node.LastProblemTime);
}
if (node.LastStatusTime != null) {
node.LastStatusTime = this.formatDateInternal(node.LastStatusTime);
}
if (node.MonitorStartTime != null) {
node.MonitorStartTime = this.formatDateInternal(node.MonitorStartTime);
}
node.CurrentStatusCssClasses.push("glyphicon");
if (node.CurrentStatus == "UpAndActive") {
node.CurrentStatusCssClasses.push("glyphicon-circle-arrow-down");
}
else if (node.CurrentStatus == "NodeOn3GBackup") {
node.CurrentStatusCssClasses.push("glyphicon-signal");
} else if (node.CurrentStatus == "UpAndActive") {
}
}
}
formatDateInternal(dateString: string) {
let utcDate = moment.utc(dateString);
let localDate = moment(utcDate).local();
return localDate.format('MM/DD/YYYY HH:mm:ss');
}
}
答案 0 :(得分:0)
您的CurrentStatusCssClasses是一个字符串数组。但是,您需要首先创建该数组(即使它是空的)。您可以在订阅事件处理程序中指定它,也可以创建CurrentStatusCssClasses类的构造函数并在那里执行。
constructor () {
this.CurrentStatusCssClasses = [];
}
答案 1 :(得分:-1)
正如tom-schier所说,CurrentStatusCssClasses不存在,你需要在将东西推入之前对其进行实例化。这就是我要做的事情:
CurrentStatusCssClassses: Array<string> = [];