【问题标题】:Module not found: Can't resolve '@reactchartjs/react-chart-2.js'找不到模块:无法解析“@reactchartjs/react-chart-2.js”
【发布时间】:2023-03-11 05:17:01
【问题描述】:

我正在尝试使用模块react-chartjs-2 来实现这个chart 我安装了模块:

npm install --save 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”

知道我做错了什么吗?

【问题讨论】:

    标签: javascript node.js reactjs npm react-chartjs


    【解决方案1】:

    以防万一有人遇到同样的错误,请这样做:

    import { Line } from "react-chartjs-2";
    

    【讨论】:

      【解决方案2】:

      我有同样的问题,这对我有用。

      yarn add react-chartjs-2 chart.js
      

      由于某种原因,npm 不起作用。

      【讨论】:

      • 这解决了我的问题。没想到 npm 不起作用。如果有人对纱线有疑问,请按照使用:stackoverflow.com/a/66239058/3921632 然后运行“纱线添加 react-chartjs-2 chart.js”
      猜你喜欢
      • 2022-11-25
      • 2020-11-15
      • 1970-01-01
      • 2021-11-03
      • 1970-01-01
      • 2023-03-22
      • 2020-07-12
      • 1970-01-01
      • 2020-01-21
      相关资源
      最近更新 更多