【发布时间】:2022-11-05 05:09:43
【问题描述】:
我尝试在我的 Django 项目中使用 Chart.js,当我使用 NPM 包时它不起作用但是当我使用 CDN 时它完美地工作
char.js 版本 3.9.1
这是我项目中的 index.html 文件
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
{# <script src="https://cdn.jsdelivr.net/npm/chart.js"></script>#}
<script type="application/json" src="/node_modules/chart.js/dist/chart.js"></script>
</head>
<body>
<canvas id="myChart" width="400" height="400"></canvas>
<script>
const ctx = document.getElementById('myChart').getContext('2d');
const myChart = new Chart(ctx, {
type: 'bar',
data: {
labels: ['Red', 'Blue', 'Yellow', 'Green', 'Purple', 'Orange'],
datasets: [{
label: '# of Votes',
data: [12, 19, 3, 5, 2, 3],
backgroundColor: [
'rgba(255, 99, 132, 0.2)',
'rgba(54, 162, 235, 0.2)',
'rgba(255, 206, 86, 0.2)',
'rgba(75, 192, 192, 0.2)',
'rgba(153, 102, 255, 0.2)',
'rgba(255, 159, 64, 0.2)'
],
borderColor: [
'rgba(255, 99, 132, 1)',
'rgba(54, 162, 235, 1)',
'rgba(255, 206, 86, 1)',
'rgba(75, 192, 192, 1)',
'rgba(153, 102, 255, 1)',
'rgba(255, 159, 64, 1)'
],
borderWidth: 1
}]
},
options: {
scales: {
y: {
beginAtZero: true
}
}
}
});
</script>
</body>
</html>
我在浏览器中的控制台错误是:
(index):17 Uncaught ReferenceError: Chart is not defined
at (index):17:17
我试图在我的脚本中使用 chart.min.js 但没有工作。 我也尝试了以前版本的 charj.js,但仍然无法正常工作。 我还在 CDN 上复制代码并将其放在本地项目中,但仍然无法正常工作。
【问题讨论】:
标签: javascript django npm chart.js