我试图了解如何将数据从父组件传递到子组件。
更具体地讲,我想将父组件移交并排列到子组件,以便将其用作图表的数据源。
我正在使用Angular 8
,mdbootstrap
(使用chart.js 2.5.0
)。我阅读了一堆教程,并且弄清楚了如何将内容从父组件传递到子组件。我可以使用推断从子模板中访问字符串等。
我的问题可能与chart.js处理其图表呈现的方式有关。
我不确定在生命周期的哪一点需要它的数据。我试图在ngOnInit
方法和AfterViewInit
方法(代码中未显示)中填充所需的变量,但这没有任何改变。
export class StatisticsComponent implements OnInit {
// business logic
public isDataLoaded = false;
public clientNumberList: string[] = new Array();
public clientNumberCountList: number[] = new Array();
// chart related stufff
public chartLabel = 'User Statistics';
public chartOptions: any = {
responsive: true,
maintainAspectRatio: false,
scales: {
xAxes: [{
scaleLabel: {
display: true,
labelString: 'Client Number'
}
}],
yAxes: [{
scaleLabel: {
display: true,
labelString: 'Count'
}
}]
}
};
constructor(private sdoUserStatisticsService: SdoUserStatisticsService) {}
ngOnInit(): void {
// get users and counts
this.sdoUserStatisticsService.getUserStatistics().subscribe(users => {
for (const user of users) {
this.clientNumberList.push(user.clientNumber);
this.clientNumberCountList.push(user.count);
}
this.isDataLoaded = true;
})
;
}
}
<div *ngIf="isDataLoaded">
<app-bar-chart [chartLabelInput]="chartLabel" [dataArrayInput]="clientNumberCountList" [labelArray]="clientNumberList" [chartOptionsInput]="chartOptions"></app-bar-chart>
</div>
export class BarChartComponent implements OnInit{
public chartType = 'bar';
@Input() chartLabelInput: string;
@Input() dataArrayInput: number[];
public chartLabel: string;
public dataArray: number[] = this.dataArrayInput;
public chartDatasets: Array<any> = [
{data: this.dataArray, label: this.chartLabel}
];
@Input() labelArray: string[];
public chartLabels: Array<any> = this.labelArray;
@Input() chartOptionsInput: any;
public chartOptions = this.chartOptionsInput;
constructor() {}
public chartClicked(e: any): void { }
public chartHovered(e: any): void { }
ngOnInit(): void {}
}
<canvas mdbChart
[chartType]="chartType"
[datasets]= "chartDatasets"
[labels]="chartLabels"
[options]="chartOptions"
[legend]=true
(chartHover)=chartHovered($event)
(chartClick)=chartClicked($event)>
</canvas>
我希望我可以访问bar-chart.component.ts中的dataArrayInput并将该数据提供给图表。 实际上,我可以使用ngOnInit方法访问此数组(代码中未显示),但不能将其用于图表。 我在浏览器控制台中收到以下错误:
ERROR TypeError: "newElm.data is undefined"
Angular 10
datasets
getDatasets
getChartBuilder
refresh
ngOnInit
checkAndUpdateDirectiveInline
checkAndUpdateNodeInline
checkAndUpdateNode
debugCheckAndUpdateNode
debugCheckDirectivesFn
View_BarChartComponent_0 BarChartComponent.html:1
Angular 30
RxJS 5
Angular 9ERROR TypeError: "newElm.data is undefined"
Angular 10
datasets
getDatasets
getChartBuilder
refresh
ngOnInit
checkAndUpdateDirectiveInline
checkAndUpdateNodeInline
checkAndUpdateNode
debugCheckAndUpdateNode
debugCheckDirectivesFn
View_BarChartComponent_0 BarChartComponent.html:1
Angular 30
RxJS 5
Angular 9
我希望有人可以帮助我:)
答案 0 :(得分:1)
我认为这是一个Angular生命周期问题。您的问题在于如何使用@Input
数据。您显然想获取dataArrayInput
附带的数据,但是访问不正确。
将取决于@Input值的变量的分配移动到ngOnInit
方法内。 @Input
变量的值在组件构造期间不可用,因此在创建因变量时,将使用undefined
初始化它们。
export class BarChartComponent implements OnInit {
public chartType = 'bar';
@Input() chartLabelInput: string;
@Input() dataArrayInput: number[];
public chartLabel: string;
public dataArray: number[];
public chartDatasets: Array < any > ;
@Input() labelArray: string[];
public chartLabels: Array < any > ;
@Input() chartOptionsInput: any;
public chartOptions;
constructor() {}
public chartClicked(e: any): void {}
public chartHovered(e: any): void {}
ngOnInit(): void {
this.dataArray = this.dataArrayInput;
this.chartDatasets = [{
data: this.dataArray,
label: this.chartLabel
}];
this.chartLabels = this.labelArray;
this.chartOptions = this.chartOptionsInput;
}
}