[英]how to use chart.js in webpacker rails 6
I installed chart.js with yarn and in application.js I call it with require('chart.js');
我用yarn安装了chart.js,在application.js中我用
require('chart.js');
调用它require('chart.js');
Then I have my js file related to one view, where I want to call new Chart
for which I get Chart is not defined.然后我有我的 js 文件与一个视图相关,我想在其中调用
new Chart
,我得到的 Chart 未定义。 So I added it as a global with所以我将它添加为全局
global.Chart = require('chart.js')
but then I get Chart is not a constructor.但后来我得到 Chart 不是一个构造函数。 How to correctly import chart.js so
Chart
will be available in any view?如何正确导入 chart.js 以便
Chart
可以在任何视图中使用? I dont want to put my js code related to chart in application.js, I want to keep it in my separated js files.我不想把我与图表相关的js代码放在application.js中,我想把它放在我分开的js文件中。
thanks谢谢
ok with trial error I found a way this is how I need to call it in application.js好的,试错了,我找到了一种方法,这就是我需要在 application.js 中调用它的方式
require ('chart.js/auto');
import Chart from 'chart.js/auto';
global.Chart = Chart;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.