该报错通常出现在使用ng2-charts插件时未正确定义chart元素的ID时。解决方法如下:
在HTML中定义一个具有唯一ID的div元素,将其用作图表容器。例如:
在component.ts文件中引入ViewChild进行注释,当视图加载完成时,从DOM中捕获ID为chartContainer的容器元素。例如:@ViewChild('chartContainer', {static: true}) chartContainer: ElementRef;
在component.ts文件中使用ngAfterViewInit生命周期函数,在该函数中将图表数据以及图表类型传递给ng2-charts,最后在chartContainer中绘制出该图表。例如:
ngAfterViewInit() { let chart = new Chart(this.chartContainer.nativeElement, { type: 'line', data: [1, 2, 3, 4, 5] }); }
import { ChartsModule } from 'ng2-charts'; @NgModule({ imports: [ChartsModule], // ... })
完成上述步骤后,刷新页面查看结果,应该可以看到成功绘制的图表。
上一篇:Angular8@angular/core/corehasnoexportedmemberafterinstallingangular-datatables
下一篇:Angular8表单输入字段禁用