最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Uncaught TypeError: Chart is not a constructor when using Chart.js - Stack Overflow

programmeradmin2浏览0评论

I'm working on my first Cordova projects and am trying to use Chart.js. The documentation states that Chart.js should be included like this:

<script src="Chart.js"></script>
<script>
    var myChart = new Chart({...})
</script>

I installed the library using bower and it is now present under www/lib/chart.js. Chrome gives me Failed to load resource: the server responded with a status of 404 when I'm loading Chart.js or lib/chart.js.

When I load lib/chart.js/src/chart.js I get a different error saying Uncaught TypeError: Chart is not a constructor in this line:

  var ctx = document.getElementById("myChart");
  var myChart = new Chart(ctx, { //...

This is what's in the file chart.js:

var Chart = require('./core/core.js')();

require('./core/core.helpers')(Chart);
// ...

window.Chart = module.exports = Chart;

Is this the right chart.js file?

I'm working on my first Cordova projects and am trying to use Chart.js. The documentation states that Chart.js should be included like this:

<script src="Chart.js"></script>
<script>
    var myChart = new Chart({...})
</script>

I installed the library using bower and it is now present under www/lib/chart.js. Chrome gives me Failed to load resource: the server responded with a status of 404 when I'm loading Chart.js or lib/chart.js.

When I load lib/chart.js/src/chart.js I get a different error saying Uncaught TypeError: Chart is not a constructor in this line:

  var ctx = document.getElementById("myChart");
  var myChart = new Chart(ctx, { //...

This is what's in the file chart.js:

var Chart = require('./core/core.js')();

require('./core/core.helpers')(Chart);
// ...

window.Chart = module.exports = Chart;

Is this the right chart.js file?

Share Improve this question edited Mar 2, 2023 at 1:02 ggorlen 57k8 gold badges110 silver badges150 bronze badges asked Jan 31, 2017 at 20:38 qwertzqwertz 3251 gold badge4 silver badges16 bronze badges
Add a comment  | 

5 Answers 5

Reset to default 5

I think that chart.js you're using has an error
try this one:

<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.4.0/Chart.min.js"></script>

On Laravel mix use:

window.Chart = require('chart.js/auto').default;

Do this and it should work import Chart from 'chart.js/auto';

https://www.chartjs.org/docs/latest/getting-started/integration.html

If you are using Chart.js v3 or higher:

import Chart from 'chart.js/auto';

If you are using Chart.js v2:

import Chart from "chart.js";

Still ran into this with chartjs 4.4.3 and webpack. In the end, using the following worked:

import { Chart, registerables } from 'chart.js';
Chart.register(...registerables);

instead of the documented:

import Chart from 'chart.js/auto'
发布评论

评论列表(0)

  1. 暂无评论