在我的 Laravel 项目中,我使用的是 adminlte 仪表板。此外,我在 adminlte 仪表板中使用 chartjs。Uncaught ReferenceError: Chart is not defined发生错误。我尝试了很多解决方案,但错误仍然相同。发生了什么问题以及如何解决这个问题?
我在chartjs文档中添加了代码示例:但这不起作用;
<canvas id="timechart" width="800px" height="400px"></canvas>
<script>
var ctx = document.getElementById('timechart').getContext('2d');
var chart = new Chart(ctx, {
// The type of chart we want to create
type: 'line',
// The data for our dataset
data: {
labels: ['January', 'February', 'March', 'April', 'May', 'June', 'July'],
datasets: [{
label: 'My First dataset',
backgroundColor: 'rgb(255, 99, 132)',
borderColor: 'rgb(255, 99, 132)',
data: [0, 10, 5, 2, 20, 30, 45]
}]
},
// Configuration options go here
options: {}
});
</script>
在主布局中添加chartjs:
<script type="text/javascript" src="{{ asset('js/Chart.js') }}"></script>
您应该将 import js 放在从 js 调用的函数上方的顶部。所以最简单的方法是放在<script type="text/javascript" src="{{ asset('js/Chart.js') }}"></script>
标题中。或者放在页脚但上面new Chart(ctx,{})
。下面是示例工作片段代码:
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/Chart.min.js"></script>
<canvas id="timechart" width="800px" height="400px"></canvas>
<script>
var ctx = document.getElementById('timechart').getContext('2d');
var chart = new Chart(ctx, {
// The type of chart we want to create
type: 'line',
// The data for our dataset
data: {
labels: ['January', 'February', 'March', 'April', 'May', 'June', 'July'],
datasets: [{
label: 'My First dataset',
backgroundColor: 'rgb(255, 99, 132)',
borderColor: 'rgb(255, 99, 132)',
data: [0, 10, 5, 2, 20, 30, 45]
}]
},
// Configuration options go here
options: {}
});
</script>
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句