繁体   English   中英

错误 TS2322:类型“字符串”不可分配给类型“keyof ChartTypeRegistry”

[英]error TS2322: Type 'string' is not assignable to type 'keyof ChartTypeRegistry'

主页.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'
  }
];

home.component.html

<canvas baseChart
    [chartType] = "lineChartType"
    [datasets] = "lineChartData"
    [labels] = "lineChartLabels"
>
</canvas>

错误:

类型“字符串”不可分配给类型“keyof ChartTypeRegistry”

错误行:

[chartType] = "lineChartType"

错误发生在home.component.html

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

仅供参考, 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

或者

npm install @types/chart.js@2.9.33

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


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

使用ChartType指定lineChartType

import { ChartType } from 'chart.js';

public lineChartType: ChartType = "line";

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM