未捕获的类型错误:使用 Chart.js 时图表不是构造函数

问题描述 投票:0回答:4

我正在开发我的第一个 Cordova 项目,并尝试使用 Chart.js。文档指出 Chart.js 应该这样包含:

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

我使用 Bower 安装了该库,它现在位于

www/lib/chart.js
下。当我加载
Failed to load resource: the server responded with a status of 404
Chart.js
时,Chrome 会给我
lib/chart.js

当我加载

lib/chart.js/src/chart.js
时,我收到一个不同的错误,在这一行中显示
Uncaught TypeError: Chart is not a constructor

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

这是文件chart.js 中的内容:

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

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

window.Chart = module.exports = Chart;

这是正确的

chart.js
文件吗?

javascript cordova webstorm chart.js chart.js2
4个回答
5
投票

我认为您使用的 Chart.js 有错误
试试这个:

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


3
投票

在 Laravel 上混合使用:

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

1
投票

这样做应该有效

import Chart from 'chart.js/auto';

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


0
投票

如果您使用 Chart.js v3 或更高版本:

import Chart from 'chart.js/auto';

如果您使用的是 Chart.js v2:

import Chart from "chart.js";
© www.soinside.com 2019 - 2024. All rights reserved.