html - chart.js ไม่มีสมาชิกที่ส่งออกชื่อ 'ChartDataSets' คุณหมายถึง 'ChartDataset' หรือไม่ ts(2724)
เมื่อฉันนำเข้า ChartDataSets ฉันได้รับข้อความแสดงข้อผิดพลาดที่ line-chart.component.ts
'chart.js' has no exported member named 'ChartDataSets'. Did you mean 'ChartDataset'?
ฉันไม่เข้าใจว่าปัญหาอยู่ตรงไหน? ฉันลืมติดตั้งไลบรารีหรือ?
โค้ดของฉันถูกนำเสนอแบบนี้ใน line-chart.component.ts
import { Component, } from '@angular/core';
import { ChartDataSets, ChartOptions } from 'chart.js';
import { Color, Label } from 'ng2-charts';
@Component({
selector: 'app-line-chart',
templateUrl: './line-chart.component.html',
styleUrls: ['./line-chart.component.css']
})
export class LineChartComponent {
lineChartData: ChartDataSets[] = [
{ data: [85, 72, 78, 75, 77, 75], label: 'Crude oil prices' },
];
lineChartLabels: Label[] = ['January', 'February', 'March', 'April', 'May', 'June'];
lineChartOptions = {
responsive: true,
};
lineChartColors: Color[] = [
{
borderColor: 'black',
backgroundColor: 'rgba(255,255,0,0.28)',
},
];
lineChartLegend = true;
lineChartPlugins = [];
lineChartType = 'line';
}
ใน line-chart.component.html ฉันมีสิ่งนี้:
<div class="chart-wrapper">
<canvas baseChart
[datasets]="lineChartData"
[labels]="lineChartLabels"
[options]="lineChartOptions"
[colors]="lineChartColors"
[legend]="lineChartLegend"
[chartType]="lineChartType"
[plugins]="lineChartPlugins">
</canvas>
</div>
จากนั้นใน app.module.ts
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { ChartsModule } from 'ng2-charts';
import { AppComponent } from './app.component';
import { LineChartComponent } from './line-chart/line-chart.component';
@NgModule({
declarations: [
AppComponent,
LineChartComponent
],
imports: [
BrowserModule,
ChartsModule
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }
ฉันขอขอบคุณคุณล่วงหน้าสำหรับความช่วยเหลือของคุณ
คำตอบ #1
สิ่งนั้นเกิดขึ้นเนื่องจากความไม่เข้ากันของเวอร์ชัน ลองเปลี่ยน ChartDataSets เป็น ChartDataSet ดู
ลองติดตั้ง npm install @types/chart.js ด้วย
นี่คือตัวอย่างการทำงานบน StackBlitz
คุณยังสามารถเห็นสิ่งนี้ได้ด้วย
คำตอบ #2
ติดตั้งแผนภูมิประเภท
npm install @types/chart.js
