在react项目中,怎么才能把dataComponent.js封装成一个工具类,然后bar.js调用?

dataComponent.js

function () {

        return
            getSupportEvents: function () {
                return ['render', 'click', 'mouseup', 'mousedown', 'mousemove', 'mouseover', 'mouseout', 'optionChange'].concat(this._getSupportEvents());
            },
            _getSupportEvents: function () {
                return [];
            },
            /**
             * 最小高度
             * @type {number}
             */
            minHeight: 10,
            /**
             * 最小宽度
             * @type {number}
             */
            minWidth: 10,
            /**
             * 初始化
             * @param {string=} id
             * @param {Object} serialize
             * @returns {exports}
             * @private
             */
            _init: function (id, serialize) {
            }

};

bar.js

import DataComponent from './dataComponent'

// 配置
var CONFIG = {
    width: 900,
    height: 700,
    color: ['#106fe1']
};

// ECharts 配置
var defaultOptions = {
    // 标题
    title: {
        text: '我的123'
    },
    tooltip: {},
    legend: { // 图例
        data:['销量']
    },
    xAxis: { // 直角坐标系 grid 中的 x 轴
        data: ["衬衫12","羊毛衫35","雪纺衫67","裤子","高跟鞋","袜子"]
    },
    yAxis: {},
    series: [{
        name: '销量123',
        type: 'bar',
        data: [5, 20, 36, 10, 10, 20]
    }]
};

/**
 * 加载数据
 * @param type
 * @param callback
 */

return DataComponent.extend({
    /**
    * 默认容器的长与宽
    * @returns {*|{}}
    * @private
    */
    _getDefaultOptions: function () {
        return _.extend(true, this._super(), {
            width: 500,
            height: 500
        });
    },

    /**
     * 初始化 ECharts 的组件
     */
    initEChart: function () {
        console.log('初始化ECharts的组件')
    },

    /**
     * 初始化
     * @private
     */
    _init: function () {
        this._super.apply(this, arguments);

        // 克隆默认 ECharts 配置
        this.defaultOptions = _.clone(defaultOptions);
        // 初始化容器长与宽
        // _.css(this.container, 'width', CONFIG.width+'px');
        // _.css(this.container, 'height', CONFIG.height+'px');
        _.css(this.container, 'width', '900px');
        _.css(this.container, 'height', '900px');
        // 初始化 ECharts 的组件
        this.initEChart();

    }

})
阅读 5.3k
2 个回答

直接定义然后导出就好了:

const dataComponent = {
    getSupportEvents(){},
    //...
}
export default dataComponent
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题