如何在webpack中使用chart.js
how to use chart.js in webpack
我在webpack中使用chart.js。当在终端运行webpack时,它是正常的,但在控制台显示错误:
"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);
});
给变量名chartjs
给require.js的回调,但是你试图在倒数第二行用Chart
引用chartjs。您需要做的是决定命名约定:
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);
});
通过将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);
});
相关文章:
- 在自定义mean.io包中使用angular-chart.js作为依赖项
- 更改使用Chart.js创建的图表中的轴线颜色
- 用chart.js绘制条形图
- chart.js 2.0为数据集添加了新属性
- Chart.js V2折线图缺点
- Chart.js极地图的比例不对
- 将gif图像添加到chart.js V2.0中
- Chart.js条形图:网格颜色和隐藏标签
- 与React Native的Chart.js等效
- Chart.js 2.1.2条形图动画问题
- Chart.js-添加渐变而不是纯色-实现解决方案
- 如何在标签上输出Chart.js雷达图的正确值
- 如何在Chart.JS 2.0上使用tooltipTemplate
- 将时间缩放添加到折线图(Chart.js)
- 是否可以使用Chart.js缩短雷达图上的外部标签,而不影响其他标签
- Chart.js折线图的0线型
- AngularJS选项卡集中的Chart.js不呈现
- Chart.js:将对象而不是int值作为数据传递
- 如何在chart.js中显示圆环图上的标签
- chart.js在我的本地主机上运行良好,但无法在线运行