简体   繁体   English

Symfony 4 - 使用 webpack encore 导入 ChartJS?

[英]Symfony 4 - Import ChartJS with webpack encore?

I downloaded the ChartJS package on Symfony, and I would like to be able to use it with webpack-encore我在 Symfony 上下载了 ChartJS 包,我希望能够将它与 webpack-encore 一起使用

In my Twig template, I can use Chart with :在我的 Twig 模板中,我可以使用 Chart :

new Chart(document.getElementById("myChart"), { ..... }

It works with CDN, but now I would like to use it with webpack-encore.它适用于 CDN,但现在我想将它与 webpack-encore 一起使用。

So in my app.js I tried :所以在我的app.js我试过:

//1er essai
import Chart from "chart.js";
global.Chart = Chart

//2eme essai
const Chart = require("chart.js");
global.Chart = Chart;

But neither works.但两者都不起作用。 Each time in the browser I get the following error :每次在浏览器中我都会收到以下错误:

Uncaught ReferenceError: Chart is not defined未捕获的 ReferenceError:图表未定义

Can someone help me please ?有人能帮助我吗 ?

When using webpack encore I find it easier to install different modules via npm.使用 webpack encore 时,我发现通过 npm 安装不同的模块更容易。 In the documentation it says that bower and npm are recommended.文档中它说推荐使用 bower 和 npm。 So you basically write this所以你基本上写这个

npm install chart.js --save

And for usage you simply import it.对于使用,您只需导入它。

If you use Yarn (recommended by Webpack Encore), just type :如果您使用 Yarn(由 Webpack Encore 推荐),只需输入:

yarn add chart.js 

Then in your js file :然后在你的 js 文件中:

import Chart from 'chart.js';

new Chart($("#myElement"), {
    ...
});

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

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