我知道在SO和其他论坛中有很多关于这个标题的问题,但没有一个能解决我的问题(我不确定是否正确(。我想使用 canvasjs 来绘制画布。我也在使用反应和新的。我的反应项目的结构如下:
+node_modules
+public
+src
-canvas.min.js
-canvas.react.js
-App.js
-index.js
-indes.css
-package.json
我的应用程序.js文件包含以下代码:
/* App.js */
import React from 'react';
import CanvasJS from './canvasjs.min';
var CanvasJSReact = require('./canvasjs.react');
var Component = React.Component;
var CanvasJS = CanvasJSReact.CanvasJS;
var CanvasJSChart = CanvasJSReact.CanvasJSChart;
class App extends Component {
render() {
const options = {
title: {
text: "Basic Column Chart in React"
},
data: [{
type: "column",
dataPoints: [
{ label: "Apple", y: 10 },
{ label: "Orange", y: 15 },
{ label: "Banana", y: 25 },
{ label: "Mango", y: 30 },
{ label: "Grape", y: 28 }
]
}]
}
return (
<div>
<CanvasJSChart options = {options}
/* onRef = {ref => this.chart = ref} */
/>
</div>
);
}
}
//module.exports = App;
export default App;
索引.js文件:
import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import App from './App';
ReactDOM.render(<App/>, document.getElementById('app'));
当我在cmd中运行npm start
时,会发生此错误:
Uncaught TypeError: Cannot assign to read only property ‘exports’ of object ‘
如果你的canvasjs是一个ES6模块,那么你应该使用
import CanvasJS from './canvasjs.min';
如果是简单的 commonJS 或 UMD 模块使用
var CanvasJS = require('./canvasjs.min');