[英]Uncaught ReferenceError: Chart is not defined
I've installed chart.js
using npm by : npm install chart.js --save-dev
, in "resources/assets/js/bootstrap.js"
我已经通过以下
chart.js
使用npm npm install chart.js --save-dev
: npm install chart.js --save-dev
,在"resources/assets/js/bootstrap.js"
npm install chart.js --save-dev
"resources/assets/js/bootstrap.js"
I refer to it by: require('chart.js');
我通过以下方式引用它:
require('chart.js');
Then in my console npm run dev
and finally it's successfully compiled in "public/js/app.js"
, however when I try to use it in my view as follow 然后在控制台
npm run dev
,最后在"public/js/app.js"
成功编译,但是当我尝试在视图中使用它时,如下所示
<script src="/js/app.js"></script><script>
var ctx = document.getElementById("myChart");
var myChart = new Chart(ctx, {
......... </script>
the browser returns 浏览器返回
Uncaught ReferenceError: Chart is not defined.
未捕获ReferenceError:未定义图表。
How come it's declared in app.js
and can't refer to it ? 它为什么在
app.js
声明而不能引用它?
Thanks in advance. 提前致谢。
If you look in app.js, is it wrapped in a function? 如果您查看app.js,它是否包装在函数中? It sounds like it's not part of the global namespace, despite being present in app.js.
听起来尽管它存在于app.js中,但它似乎不是全局名称空间的一部分。
If you're using es6 you might need to change the way you require it. 如果您使用的是es6,则可能需要更改所需的方式。
from the docs: http://www.chartjs.org/docs/ 来自文档: http : //www.chartjs.org/docs/
// Using CommonJS
var Chart = require('chart.js')
var myChart = new Chart({...})
// ES6
import Chart from 'chart.js'
let myChart = new Chart({...})
// Using requirejs
require(['path/to/Chartjs'], function(Chart){
var myChart = new Chart({...})
})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.