[英]how to use chart.js in webpack
I'm using chart.js in webpack. 我在webpack中使用chart.js。 when run webpack in terminal it's ok but in console display Error: 在终端中运行webpack时可以,但是在控制台中显示错误:
"Uncaught ReferenceError: Chart is not defined" “未捕获的ReferenceError:未定义图表”
require(['jquery', 'chartjs'], function($, chartjs) {
console.log('aaaaaaaa');
var riceData = {
labels : ["January","February","March","April","May","June"],
datasets :
[
{
fillColor : "rgba(172,194,132,0.4)",
strokeColor : "#ACC26D",
pointColor : "#fff",
pointStrokeColor : "#9DB86D",
data : [203000,15600,99000,25100,30500,24700]
}
]
}
var rice = document.getElementById('myChart').getContext('2d');
new Chart(rice).Line(riceData);
});
You give the variable name chartjs
to require.js's callback, yet you try to reference chartjs with Chart
on the second-to-last line. 您将变量名称chartjs
为require.js的回调,但是尝试使用倒数第二行的Chart
引用chartjs。 What you would need to do is decide on a naming convention: 您需要做的是确定命名约定:
require(['jquery', 'chartjs'], function($, Chart) {
console.log('aaaaaaaa');
var riceData = {
labels : ["January","February","March","April","May","June"],
datasets :
[
{
fillColor : "rgba(172,194,132,0.4)",
strokeColor : "#ACC26D",
pointColor : "#fff",
pointStrokeColor : "#9DB86D",
data : [203000,15600,99000,25100,30500,24700]
}
]
}
var rice = document.getElementById('myChart').getContext('2d');
new Chart(rice).Line(riceData);
});
The problem was resolved by changing require
to define
And also you should use the npm package manager. 通过更改require
define
解决了该问题,并且还应该使用npm软件包管理器。
define(['jquery', 'chartjs'], function($, chartjs) {
var riceData = {
labels : ["January","February","March","April","May","June"],
datasets :
[
{
fillColor : "rgba(172,194,132,0.4)",
strokeColor : "#ACC26D",
pointColor : "#fff",
pointStrokeColor : "#9DB86D",
data : [203000,15600,99000,25100,30500,24700]
}
]
}
var rice = document.getElementById('myChart').getContext('2d');
new Chart(rice).Line(riceData);
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.