Angular2 @ViewChild 没有从 ng2-charts 中获取 BaseChartDirective

Angular2 @ViewChild is not picking up BaseChartDirective from ng2-charts

我正在使用 ng2-charts v1.5.0,我正在尝试让图表在单击后更新为新数据。我正在关注其他人发布的示例。

在我看来我有:

<div style="display: block">

  <canvas #chart baseChart
              [data]="dChartData"
              [labels]="dChartLabels"
              [chartType]="dChartType"
              (chartClick)="chartClicked($event)">
  </canvas>

</div>

如果我写:

@ViewChild( BaseChartDirective ) chart: BaseChartDirective;

然后我得到:

__zone_symbol__error : Error: Can't construct a query for the property "chart" of "ChartTest1WidgetComponent" since the query selector wasn't defined.

如果我写:

@ViewChild( 'chart' ) chart: BaseChartDirective;

然后我调用 this.chart.ngOnChanges({});,但我得到:_this.chart.ngOnChanges is not a function

这对我来说没有意义。如何获得引用 BaseChartDirective 的正确视图子项?

这对我有用: @ViewChild(BaseChartDirective) private Chart: BaseChartDirective; 然后 this.Chart.ngOnChanges({} as SimpleChanges).

但是,更简单的方法是实际使用新数据更新变量 dChartData,它会自动使用新数据更新您的图表。它比使用 ngOnChanges 更快。希望这对您有所帮助!