简体   繁体   English

未捕获的ReferenceError:未定义图表

[英]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-devnpm 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.

相关问题 “未捕获的ReferenceError:图表未定义” - “Uncaught ReferenceError: chart is not defined” Chart.js - 未捕获的参考错误:未定义图表 - Chart.js - Uncaught ReferenceError: chart is not defined 未捕获的ReferenceError:未定义require - Chart.js - Uncaught ReferenceError : require is not defined - Chart.js 未捕获的ReferenceError:未定义google(Google图表) - Uncaught ReferenceError: google is not defined (Google Chart) 未捕获的 ReferenceError:Laravel mix 中未定义图表 - Uncaught ReferenceError: Chart is not defined in Laravel mix 未捕获的ReferenceError:未定义图表-Laravel 5中的Chart.js - Uncaught ReferenceError: Chart is not defined - Chart.js in laravel 5 未捕获的ReferenceError:使用两个图表时未定义图表 - Uncaught ReferenceError: chart is not defined when using two charts Uncaught ReferenceError:未定义图表,或如何在Twig中包含javascript - Uncaught ReferenceError: Chart is not defined OR how to include javascript in Twig 未捕获的 ReferenceError:图表未在 test.html:11 中定义 - Uncaught ReferenceError: Chart is not defined at test.html:11 “未定义(承诺)ReferenceError:未定义January(Ru)” Google图表 - “Uncaught (in promise) ReferenceError: January(Ru) is not defined” Google chart
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM