简体   繁体   中英

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"
I refer to it by: 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

<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.

How come it's declared in app.js and can't refer to it ?
Thanks in advance.

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.

If you're using es6 you might need to change the way you require it.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM