未捕获的TypeError TypeError:Highcharts.chart不是函数

zz2j4svz  于 9个月前  发布在  Highcharts
关注(0)|答案(1)|浏览(110)

我的代码中出现了这个错误:

TypeError: Highcharts.chart is not a function
    at Chart (file:///C:/Users/Mon%20PC/Desktop/Le%20bot/highcharts/index.js:7:24)
    at file:///C:/Users/Mon%20PC/Desktop/Le%20bot/highcharts/index.js:19:13
    at ModuleJob.run (node:internal/modules/esm/module_job:194:25)

错误代码:https://hastebin.com/share/ozijerocem.perl
完整代码:

import Highcharts from 'highcharts/highcharts.src.js';

// Load the exporting module.
import 'highcharts/modules/exporting.src.js';

export default function Chart() {
  const c = Highcharts.chart({
    title: {
      text: 'My custom title'
    },
    subtitle: {
        text: 'My custom subtitle'
    }
  });

  return c.getSVG();
}

console.log(Chart());

如果我使用highcharts/es-modules/masters/highcharts.src.jshighcharts/es-modules/masters/modules/exporting.src.js,我得到:TypeError: Cannot read properties of undefined (reading 'hasOwnProperty')
如果我使用highcharts/es5/highcharts.src.jshighcharts/es5/modules/exporting.src.js,我会得到相同的错误:TypeError: Highcharts.chart is not a function

Node.js版本:18.16.0
我不使用Typescript,只使用JavaScript
Highcharts版本:11.1.0

在网上工作,但不是在我的项目中... https://pdkxxv-1234.csb.app/
使用node.js的沙盒,发生相同的错误(除了options之外,还使用renderTocallback进行了测试):https://replit.com/@PauLem79/stackoverflowerrorhighcharts

我在我的discord服务器上使用highcharts发送图表,我不在网络上使用它!

在Node.js v18、v16和v12上尝试:相同错误

vuv7lop3

vuv7lop31#

正如您在API(https://api.highcharts.com/class-reference/Highcharts.Chart#Chart)中看到的,Highcharts.chart函数有三个参数:

  • renderTo-要呈现的DOM元素或其id
  • options-图表选项
  • callback-在图表创建后运行的可选函数

在您的示例中,它缺少renderTo参数,因此Highcharts没有任何可以呈现图表的容器。
我在这里重复了这个问题:https://jsfiddle.net/BlackLabel/cbfuhkj1/
如果你打开控制台,你会注意到Highcharts抛出了一个错误(错误#13 -https://assets.highcharts.com/errors/13/)。
我认为这是它不起作用的主要原因。

如果添加renderTo参数,一切似乎都很好:https://jsfiddle.net/BlackLabel/mtr67aef/

如果这不能解决您的问题,请尝试在JSFiddle或CodeSandbox等在线代码编辑器中重现此错误,以便我可以再次查看并提出另一种解决方案。

其他原因:

您没有正确导入Highcharts模块。
要导入Highcharts,我们用途:import Highcharts from 'highcharts'和导入一个模块和使用我们做:

import ExportingModule from 'highcharts/modules/exporting';
ExportingModule(Highcharts);

相关问题