如何使用Webpack导入Chart.js

mpbci0fu  于 2022-11-06  发布在  Chart.js
关注(0)|答案(3)|浏览(180)

我有一个使用Webpack的Vue JS项目,需要导入Chart.js。我尝试过

// import Chart from 'chart.js/Chart.min.js'

这会在尝试使用库时出现js错误。

moiiocjp

moiiocjp1#

在你做了npm install chart.js之后,
您只需使用

import Chart from 'chart.js'

导入chart.js希望能有所帮助。

1l5u6lss

1l5u6lss2#

this issue上所示,如果使用Angular CLI,则只需将其添加到angular-cli.json中的脚本数组:

"../node_modules/chart.js/dist/Chart.bundle.min.js"

重新启动您的应用程序,实时重新加载无法完成此任务。

fjnneemd

fjnneemd3#

使用webpack时,我发现必须将Chart添加到全局上下文中,如下所示:

// Ensure the Chart class is loaded in the global context
import Chart from 'chart.js/auto';
window.Chart = Chart

相关问题