gpt4 book ai didi

reactjs - 无法解析 'chart.js/auto'

转载 作者:行者123 更新时间:2023-12-02 16:12:12 26 4
gpt4 key购买 nike

我正在尝试使用 chart-js 和 react-chartjs-2 但显示以下错误:

编译失败。

./node_modules/react-chartjs-2/dist/index.modern.js
Module not found: Can't resolve 'chart.js/auto' in 'C:\Users\An\chart-react\project-folder\client\node_modules\react-chartjs-2\dist'

我的代码:

import React from "react";
import { Bar } from "react-chartjs-2";

function ChartData() {
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,
},
},
],
},
};

return (
<div>
<Bar data={data} options={options} />
</div>
);
}

export default ChartData;

已安装的依赖项:

"图表.js": "^2.9.4",“react-chartjs-2”:“^3.0.3”

最佳答案

安装最新版本的 chart.js 应该可以解决这个问题。

# with npm
npm install chart.js@latest

# with yarn
yarn add chart.js@latest

这会将 chart.js 更新到最新版本(当前为 3.3.0)。

我相信只要 chart.js 主要版本是 3+,它就可以工作。


这是一个有效的 Demo安装了最新版本的 chart.js


更多笔记

您使用的是 chart.js 的 v2,但是 react-chartjs-2 的 v3。

react-chartjs-2 peer dependencies指定使用 chart.js 版本 ^3.1.0。他们还提到在 README 中使用 v3 的 chart.js .

如果您可以使用最新版本的 chart.js,则上面的 npmyarn 安装命令应该足够了。

关于reactjs - 无法解析 'chart.js/auto',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67675956/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com