Javascript chart.js v3,扩展控制器或任何基类提供ie:class构造函数BarController在没有';新的';?
我在一个gatsby+React项目中,直接使用chart.js库(即不是React库)。每当我尝试从chart.js扩展基类时,都会出现以下错误: TypeError:在没有“new”的情况下无法调用类构造函数BarController 我的代码是:Javascript chart.js v3,扩展控制器或任何基类提供ie:class构造函数BarController在没有';新的';?,javascript,reactjs,chart.js,chart.js3,Javascript,Reactjs,Chart.js,Chart.js3,我在一个gatsby+React项目中,直接使用chart.js库(即不是React库)。每当我尝试从chart.js扩展基类时,都会出现以下错误: TypeError:在没有“new”的情况下无法调用类构造函数BarController 我的代码是: import { BarController } from 'chart.js'; export class CustomBarController extends BarController { defaults = {
import { BarController } from 'chart.js';
export class CustomBarController extends BarController {
defaults = {
datasetElementType: null,
dataElementType: null
}
id = 'CustomBar'
draw() {
// Call bubble controller method to draw all the points
super.draw(arguments);
// Now we can do some custom drawing for this dataset. Here we'll draw a red box around the first point in each dataset
const meta = this.getMeta();
const pt0 = meta.data[0];
const {x, y} = pt0.getProps(['x', 'y']);
const {radius} = pt0.options;
const ctx = this.chart.ctx;
ctx.save();
ctx.strokeStyle = 'red';
ctx.lineWidth = 1;
ctx.strokeRect(x - radius, y - radius, 2 * radius, 2 * radius);
ctx.restore();
}
}
后来我用它:
import { Chart, registerables } from 'chart.js';
import { CustomBarController } from './CustomBarController';
Chart.register(...registerables);
Chart.register(CustomBarController);
let chart = new Chart(ctx, { type: 'CustomBar', data: data, options: props.options });
然而,它总是给出错误
有人认为这是盖茨比的事吗?我是否需要以不同的方式导入chart.js子库?
或者有没有人在盖茨比项目中有过在重写基类时执行chart.js的经验?我已经证实,这是盖茨比的事情。将代码迁移到常规生成时不会发生。