我正在尝试使用模块react-chartjs-2实现此图表我安装了该模块:
npm安装--保存react-chartjs-2 chart.js
所以我在package.json中有这样的代码:
"chart.js": "^2.9.4",
"react-chartjs-2": "^2.11.1"
我从GitHub复制了图表代码:
import React from 'react'
import { Line } from '@reactchartjs/react-chart.js'
const data = {
labels: ['1', '2', '3', '4', '5', '6'],
datasets: [
{
label: '# of Votes',
data: [12, 19, 3, 5, 2, 3],
fill: false,
backgroundColor: 'rgb(255, 99, 132)',
borderColor: 'rgba(255, 99, 132, 0.2)',
},
],
}
const options = {
scales: {
yAxes: [
{
ticks: {
beginAtZero: true,
},
},
],
},
}
const LineChart = () => (
<>
<div className='header'>
<h1 className='title'>Line Chart</h1>
<div className='links'>
<a
className='btn btn-gh'
href='https://github.com/reactchartjs/react-chartjs-2/blob/react16/example/src/charts/Line.js'
>
Github Source
</a>
</div>
</div>
<Line data={data} options={options} />
</>
)
export default LineChart
但是,我一直遇到这样的错误:
找不到模块:无法解析'@reactchartjs/react-chart-2.js'
知道我做错了什么吗?
万一有人面临同样的错误,只需这样做:
import { Line } from "react-chartjs-2";