我正在尝试将javascript标尺脚本(http://bernii.github.io/gauge.js/dist/gauge.min.js)导入我的组件中以供使用。

我将脚本从(http://bernii.github.io/gauge.js/dist/gauge.min.js)复制到重命名为(Gauge.js)的组件文件夹中,并需要它。

var Gauge = require('./Gauge');


然后,我尝试使用像这样的构造函数来创建仪表脚本,这在componentDidMount()中使用。

var target = this.refs.test; var gauge = new Gauge(target);

我的渲染

render() { return( <div className="GaugeTest"> <canvas width={this.props.width} height={this.props.height} ref="test" /> </div> ); }

我得到错误:

Overview.js:34未捕获的TypeError:仪表不是构造函数这是指上面带有以下代码的行:

var gauge = new Gauge(target);


关于如何包含/要求此脚本,以便可以在组件中使用的任何想法?

最佳答案

系统会将其导入为一个以Gauge(及其他选项)为键的对象。所以改为:

var Gauge = Gauge.Gauge(target);


如果您想了解为什么只是console.log(Gauge)

关于javascript - 将Gauge.js导入React组件,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/44200529/

10-11 14:14