Javascript 角度画布未定义
我在ionic angular应用程序中使用了Javascript 角度画布未定义,javascript,angular,typescript,chart.js,Javascript,Angular,Typescript,Chart.js,我在ionic angular应用程序中使用了chart.js,我得到了以下错误: this.barCanvas is undefined 代码 HTML <ion-card-content> <canvas #barCanvas></canvas> </ion-card-content> 有什么想法吗?我不懂,但是 我确信ionViewDidEnter函数在ngAfterviewInit角度组件的生命周期挂钩之前运行 ViewChild仅
chart.js
,我得到了以下错误:
this.barCanvas is undefined
代码
HTML
<ion-card-content>
<canvas #barCanvas></canvas>
</ion-card-content>
有什么想法吗?我不懂,但是 我确信
ionViewDidEnter
函数在ngAfterviewInit
角度组件的生命周期挂钩之前运行
ViewChild
仅在ngAfterViewInit
生命周期挂钩中/之后初始化或访问实例
因此,您必须确保ionViewDidEnter
运行一次viewChild
实例,或者确保从ngAfterViewInit
函数运行barChartFunction
函数。
我已经编译了micronyksanswer和其他代码,现在可以使用了
我是这样做的:
// Pay attention to added `read: ElementRef` (the most important part is this code
@ViewChild('barCanvas', {static: false, read: ElementRef}) barCanvas: ElementRef;
然后我将AfterViewInit
添加到我的组件中,并将加载代码放入该组件中
ngAfterViewInit() {
setTimeout(() => {
this.barChartFunction();
}, 6000);
}
barChartFunction() {
// my charts code...
}
希望它能为那些需要帮助的人带来方便。尽我所能感谢
ionViewDidEnter
与ngAfterviewInit
相同,或者我可能是错误的来源
1-2-
ngAfterViewInit() {
setTimeout(() => {
this.barChartFunction();
}, 6000);
}
barChartFunction() {
// my charts code...
}