laravel 未捕获的 ReferenceError:图表未定义

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/43097775/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 15:40:45  来源:igfitidea点击:

Uncaught ReferenceError: Chart is not defined

javascriptphplaravelnpmchart.js

提问by Yamen Ashraf

I've installed chart.jsusing npm by : npm install chart.js --save-dev, in "resources/assets/js/bootstrap.js"
I refer to it by: require('chart.js');
Then in my console npm run devand finally it's successfully compiled in "public/js/app.js", however when I try to use it in my view as follow

我已经chart.js通过 : 使用 npm 进行安装npm install chart.js --save-dev,在"resources/assets/js/bootstrap.js"
我通过以下方式引用它: require('chart.js');
然后在我的控制台中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.jsand can't refer to it ?
Thanks in advance.

为什么它被声明为 inapp.js并且不能引用它?
提前致谢。

采纳答案by dersam

If you look in app.js, is it wrapped in a function? It sounds like it's not part of the global namespace, despite being present in app.js.

如果您查看 app.js,它是否包含在一个函数中?听起来它不是全局命名空间的一部分,尽管它存在于 app.js 中。

回答by Jonathan Wood

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({...})
})