【问题标题】:CanvasJs React - Cannot assign to read only property 'exports' of objectCanvasJs React - 无法分配给对象的只读属性“导出”
【发布时间】:2018-11-29 20:45:04
【问题描述】:

我想第一次使用 CanvasJs React 并按照link 的指南进行操作。

链接显示三个步骤:

  1. 创建一个 React 应用程序
  2. 在您的应用中保存 CanvasJS React 文件(从 link 下载)
  3. 导入 CanvasJS React 组件

我已经完成了上面的三个步骤,但是在完成最后一个步骤后,我在网页上收到了这个错误:

TypeError: Cannot assign to read only property 'exports' of object '#<Object>'

Module.<anonymous>
src/assets/js/canvasjs.react.js:42
  39 |  }   
  40 | }
  41 | 
> 42 | module.exports = {
  43 |     CanvasJSChart: CanvasJSChart,
  44 |     CanvasJS: CanvasJS
  45 | };

canvasjs.react.js:

var React = require('react');
var CanvasJS = require('./canvasjs.min');

class CanvasJSChart extends React.Component {
    static _cjsContainerId = 0
    constructor(props) {        
        super(props);       
        this.options = props.options ? props.options : {};      
        this.containerProps = props.containerProps ? props.containerProps : {width: "100%", position: "relative"};
        this.containerProps.height = props.containerProps && props.containerProps.height ? props.containerProps.height : this.options.height ? this.options.height + "px" : "400px";
        this.chartContainerId = "canvasjs-react-chart-container-" + CanvasJSChart._cjsContainerId++;
    }   
    componentDidMount() {
        //Create Chart and Render
        this.chart = new CanvasJS.Chart(this.chartContainerId, this.options);
        this.chart.render();

        if(this.props.onRef)
            this.props.onRef(this.chart);
    }   
    shouldComponentUpdate(nextProps, nextState){
        //Check if Chart-options has changed and determine if component has to be updated
        return !(nextProps.options === this.options);
    }
    componentDidUpdate() {
        //Update Chart Options & Render
        this.chart.options = this.props.options;
        this.chart.render();
    }
    componentWillUnmount() {
        //Destroy chart and remove reference
        this.chart.destroy();
        if(this.props.onRef)
            this.props.onRef(undefined);
    }       
    render() {      
        //return React.createElement('div', { id: this.chartContainerId, style: this.containerProps });     
        return <div id = {this.chartContainerId} style = {this.containerProps}/>        
    }   
}

module.exports = {
    CanvasJSChart: CanvasJSChart,
    CanvasJS: CanvasJS
};

App.js:

import React, { Component } from 'react';
var CanvasJSReact = require('./assets/js/canvasjs.react');
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>
        );
    }
}

export default App;

我正在使用 create-react-app v1.5.2:

react: 16.6.3,
react-dom: 16.6.3,
react-scripts: 2.1.1

nodejs: v8.13.0
npx: v6.4.1

有人遇到过像我这样的问题吗?

【问题讨论】:

    标签: reactjs canvasjs


    【解决方案1】:

    我遇到了同样的问题,我通过将 canvasjs.react.js 的结束语法切换为:

    export {CanvasJSChart, CanvasJS};
    

    并在 App.js 中导入画布:

    import CanvasJSReact from './canvasjs.react';
    import { CanvasJS } from './canvasjs.react';
    import { CanvasJSChart} from './canvasjs.react';
    

    而不是使用require。希望对您有所帮助!

    【讨论】:

    • 你好,当你完成切换后,你就不需要这个了import CanvasJSReact from './canvasjs.react';
    【解决方案2】:

    这个问题似乎是由于 v2.2.0-rc.5 中引入的 Webpack 中的重大更改。这是您分享的Sample ProjectUpdated Code (Link 2)。

    【讨论】:

    • 非常感谢您的帮助
    【解决方案3】:

    根据Webpack,不能混用importmodule.exports。尝试仅使用 require() 语句。

    【讨论】:

    • 感谢您的回答,我已经使用 require() 声明 CanvasJSReact 并尝试将 import React, { Component } from 'react'; 更改为 var React = require('react'); 但仍然得到相同的错误输出。
    • 你用过import的应用中的所有地方你都替换了吗?
    • 是的,我在新项目中使用 create-react-app 创建了这个,它只生成 2 个包含 import 语句(App.js 和 index.js)的默认文件。我已将 2 个文件中的所有 import 语句替换为 require () 语句,但仍然得到相同的错误输出。
    • 但是你还在用export default吗?经验法则:import 搭配 exportrequire() 搭配 module
    • 我尝试将App.js 中的App.js 中的export 更改为module.exports,例如canvasjs.react.js 文件,但仍然出现同样的错误。我真的很困惑。
    猜你喜欢
    • 1970-01-01
    • 2020-06-26
    • 2021-06-30
    • 2018-11-21
    • 1970-01-01
    • 1970-01-01
    • 2020-02-17
    • 1970-01-01
    • 2018-07-30
    相关资源
    最近更新 更多