Angular 如何使用typescript访问数组内部的属性
我有一个属性数组,当我尝试进行循环时,我可以在日志中看到对象,但是如果我尝试访问它们,我会出错 这是我的班级:Angular 如何使用typescript访问数组内部的属性,angular,typescript,Angular,Typescript,我有一个属性数组,当我尝试进行循环时,我可以在日志中看到对象,但是如果我尝试访问它们,我会出错 这是我的班级: export class M{ id: number; carName : string; constructor(Id:number,CarName:string){ this.id = Id; this.carName = CarName; } } export class C{ Values: M[]; } 因此,当我记录this.C.Values时,
export class M{
id: number;
carName : string;
constructor(Id:number,CarName:string){
this.id = Id;
this.carName = CarName;
}
}
export class C{
Values: M[];
}
因此,当我记录this.C.Values
时,我可以看到它,但是如果我尝试访问它,我就没有定义
this.httpClient.getC().subscribe(x =>
{
this.C = x;
console.log(this.C.Values);
})
即使循环我也得到了同样的结果
this.Data = this.C.Values;
this.Data.forEach(element => {
console.log(element);
});
})
在使用之前,您需要声明属性:
export class C {
Values: M[] = new Array<M>()
}
导出C类{
值:M[]=新数组()
}
在使用属性之前,您需要声明属性:
export class C {
Values: M[] = new Array<M>()
}
导出C类{
值:M[]=新数组()
}
如果执行console.log(x.Values)
,您看到什么了吗?无论如何,如果您执行console.log(x)
,请将您在控制台中看到的内容添加到您的问题中。这种问题是某种瘟疫,我在过去几天多次看到它,您需要声明值:M[]=[]
要能够使用itI do see.M的数组,有什么错误?如果我使用element.Value,我就没有定义。如果使用console.log(x.Values)
,你会看到什么吗?无论如何,如果您执行console.log(x)
,请在您的问题中添加您在控制台中看到的内容。这种问题是一种瘟疫,我在过去几天多次看到过它。您需要声明值:M[]=[]
才能使用itI do see.M的数组。错误是什么?如果我执行元素。值我未定义