I'm using chart.js in webpack. when run webpack in terminal it's ok but in console display Error:
"Uncaught ReferenceError: Chart is not defined"
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. 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.
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);
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.