错误 TS2322:类型“string”无法分配给类型“keyof ChartTypeRegistry”

Mah*_*uvo 8 typescript chart.js ng2-charts angular

home.component.ts

global: boolean = false;
country!: string;
data: GlobalData;
dailyData: any[] = [];
countries: any[] = [];
lineChartData: any[] = [
  {
    data: [65, 64, 33, 44],
    label: 'Temp label'
  }
];
lineChartType = "line";
lineChartLabels: any[] = [
  'label01', 'label02', 'label03'
];
barChartData: any[] = [
  {
      data: [65, 76, 33],
      label: 'Label'
  }
];
Run Code Online (Sandbox Code Playgroud)

home.component.html

<canvas baseChart
    [chartType] = "lineChartType"
    [datasets] = "lineChartData"
    [labels] = "lineChartLabels"
>
</canvas>
Run Code Online (Sandbox Code Playgroud)

错误:

类型“string”不可分配给类型“keyof ChartTypeRegistry”

错误行:

[chartType] = "lineChartType"
Run Code Online (Sandbox Code Playgroud)

错误发生在 home.component.html

我需要一个解决方案。尝试在谷歌上找到解决方案,但我找不到。

Yon*_*hun 15

仅供参考,ChartTypeRegistry是由 Chart.js 在3.0 及更高版本中引入的。

因此,我得出结论,您安装的 Chart.js 与最新的 ng2-chart(版本 2.4.2)不兼容。

为了重现相同的问题,我安装了Chart.js 版本 3.4,链接如下:

示例项目(chart.js v 3.4


解决方案(chart.js 3.0之前版本)

根据ng2-charts - npm

您需要使用以下命令安装 Chart.js:

npm install chart.js@2.9.4
Run Code Online (Sandbox Code Playgroud)

或者

npm install @types/chart.js@2.9.33
Run Code Online (Sandbox Code Playgroud)

示例解决方案(chart.js v 2.9.4


解决方案(chart.js 3.0 及更高版本)

lineChartType用类型指定ChartType

npm install chart.js@2.9.4
Run Code Online (Sandbox Code Playgroud)

示例解决方案(chart.js v 3.0