Angular web应用程序-类型脚本http数据问题
我正在使用Angular和SpringMVC框架构建一个webapp。 我正在尝试加载一个用户列表(代码中名为“consurenti”)。来自后端的http请求一切正常,但当我尝试在前端部分使用TypeScript处理数据时,我不断遇到如下错误: 无法设置未定义的属性“headerRow” 在TableComponent.ngonit(网页包-internal:///./src/app/tables/tables.component.ts:27 无法读取未定义的属性“headerRow” 无法读取未定义的属性“push” 其他属性也是如此 我试图使用一个自由的角度仪表板客户端作为模板,在表中显示数据 下面是代码 表3.1.tsAngular web应用程序-类型脚本http数据问题,angular,typescript,Angular,Typescript,我正在使用Angular和SpringMVC框架构建一个webapp。 我正在尝试加载一个用户列表(代码中名为“consurenti”)。来自后端的http请求一切正常,但当我尝试在前端部分使用TypeScript处理数据时,我不断遇到如下错误: 无法设置未定义的属性“headerRow” 在TableComponent.ngonit(网页包-internal:///./src/app/tables/tables.component.ts:27 无法读取未定义的属性“headerRow” 无法读
export class TablesComponent implements OnInit {
public consulenti: Consulente[];
public consulentiData: TableData;
private rows: number;
private cols: number;
constructor(private dataService: DataService) {} // Service injection
setRowData() {
if (this.rows != undefined && this.cols != undefined) {
for (let i = 0; i < this.cols; i++) this.consulentiData.dataRows[i] = this.consulenti[i].stringify();
}
// else error
}
ngOnInit() {
this.consulentiData.headerRow = ['Id', 'Nome', 'Cognome', 'E-mail', 'Scadenza contratto'];
this.dataService.getConsulenti().subscribe(data => {
for (let i = 0; i < data.length; i++) this.consulenti.push(data[i]);
});
this.rows = this.consulenti.length;
this.cols = this.consulenti[0].stringifyFields;
this.consulentiData.headerRow = ['Id', 'Nome', 'Cognome', 'E-mail', 'Scadenza contratto'];
this.setRowData();
}
定义如下:
public consulenti: any[] = [];
public consulentiData:any = {};
编辑
访问订阅的数据:
ngOnInit() {
this.consulentiData.headerRow = ['Id', 'Nome', 'Cognome', 'E-mail', 'Scadenza contratto'];
this.dataService.getConsulenti().subscribe(data => {
this.consulenti = data;
this.rows = this.consulenti.length;
this.cols = this.consulenti[0].stringifyFields;
this.consulentiData.headerRow = ['Id', 'Nome', 'Cognome', 'E-mail', 'Scadenza contratto'];
this.setRowData();
});
}
public ConsultentiData:TableData;->undefined this.ConsultentiData.headerRow=this.undefined.headerRow->无法设置undefined的属性'headerRow',如Prachi所说,您需要先初始化字段。谢谢!现在我遇到了一些其他错误。它似乎可以从订阅中获取数据。在调试器中,数据是复制,但当函数结束时,数组“consurenti”结果为空。你能帮我吗?非常感谢。不幸的是,这不起作用。谢谢你的帮助。非常感谢!
public consulenti: any[] = [];
public consulentiData:any = {};
ngOnInit() {
this.consulentiData.headerRow = ['Id', 'Nome', 'Cognome', 'E-mail', 'Scadenza contratto'];
this.dataService.getConsulenti().subscribe(data => {
this.consulenti = data;
this.rows = this.consulenti.length;
this.cols = this.consulenti[0].stringifyFields;
this.consulentiData.headerRow = ['Id', 'Nome', 'Cognome', 'E-mail', 'Scadenza contratto'];
this.setRowData();
});
}