简体   繁体   English

如何在代码和框中使用 require(chart.js)

[英]how to use require(chart.js) in codesandbox

I was trying to do this in a codesandbox:我试图在代码沙箱中做到这一点:

const Chart = require("chart.js");
  plot() {
    this.chart = new Chart(this.chartRef.current, {
     ..
          }
        ]
      }
    });
  }

However, I get an error on require:但是,我在需要时收到错误:

Cannot find name 'require'.

and that Chart is not a constructor并且Chart is not a constructor

I also tried replacing it with我也尝试用

import Chart from chart.js

but that also doesnt work.但这也行不通。 How can I fix my codesandbox?如何修复我的代码框? Here's a link这是一个链接

https://codesandbox.io/s/opur8?file=/src/Graph.tsx:2009-2333https://codesandbox.io/s/opur8?file=/src/Graph.tsx:2009-2333

To load and register everything, try:要加载和注册所有内容,请尝试:

import Chart from 'chart.js/auto';

...which is short for... ...这是...的缩写

import { Chart, registerables } from 'chart.js';
Chart.register(...registerables);

...which in turn is short for importing and registering every part individually. ...这反过来又是单独导入和注册每个部分的缩写。 See the documentation for more examples.有关更多示例,请参阅文档

Chart class is exported as a named export. Chart class 导出为命名导出。

Use: import {Chart} from 'chart.js'使用: import {Chart} from 'chart.js'

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

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