鍍金池/ 問(wèn)答/HTML/ 我想把bar.js文件改成react項(xiàng)目中import形式,怎么能實(shí)現(xiàn)?exte

我想把bar.js文件改成react項(xiàng)目中import形式,怎么能實(shí)現(xiàn)?extend和_init前面 _ 怎么理解?

bar.js

define(['require', '../dataComponent', '../../util/helper', 'echarts'], function (require, DataComponent, _, ECharts) {
    // 配置
    var CONFIG = {
        width: 900,
        height: 700,
        color: ['#106fe1']
    };

    // ECharts 配置
    var defaultOptions = {
        // 標(biāo)題
        title: {
            text: '我的123'
        },
        tooltip: {},
        legend: { // 圖例
            data:['銷量']
        },
        xAxis: { // 直角坐標(biāo)系 grid 中的 x 軸
            data: ["襯衫12","羊毛衫35","雪紡衫67","褲子","高跟鞋","襪子"]
        },
        yAxis: {},
        series: [{
            name: '銷量123',
            type: 'bar',
            data: [5, 20, 36, 10, 10, 20]
        }]
    };
    /**
     * 加載數(shù)據(jù)
     * @param type
     * @param callback
     */
    return DataComponent.extend({

        /**
         * 默認(rèn)容器的長(zhǎng)與寬
         * @returns {*|{}}
         * @private
         */
        _getDefaultOptions: function () {
            return _.extend(true, this._super(), {
                width: 500,
                height: 500
            });
        },

        /**
         * 初始化 ECharts 的組件
         */
        initEChart: function () {
            this.echart = ECharts.init(this.container); // 容器大小
            this.echart.setOption(this.defaultOptions);
        },

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

            // 克隆默認(rèn) ECharts 配置
            this.defaultOptions = _.clone(defaultOptions);
            // 初始化容器長(zhǎng)與寬
            // _.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();

        },

    })
});

dataComponent.js

define([
    '../util/helper',

], function (_) {

    return Optionable.extend({
        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) {

            var self = this;

            if (arguments.length < 2) {
                serialize = id || {};
                id = _.getUUID();
            }

            serialize = self.originSerialize = serialize || {};

            self.origin = serialize.options || {};

            /**
             * 組件ID
             * @type {string}
             */
            self.id = id;

            /**
             * container class
             * @type {string}
             */
            self.domClass = 'component component-' + self.type;

            /**
             * container uuid
             * @type {string}
             */
            self.domId = 'component-' + self.id;

            /**
             * @type {HTMLElement} 組件容器
             */
            self.container = _.DOM.div({class: self.domClass, id: self.domId, 'data-component': self.id});

            // 綁定組件實(shí)例到容器上
            self.getContainer().__component__ = self;

            self._super();

            self.EVENTS = self.getSupportEvents();

            // 綁定DOM元素事件
            _.each(['click', 'dblclick', 'mouseover', 'mousemove', 'mouseout', 'mousedown', 'mouseup'], function (event) {
                _.addEvent(self.getContainer(), event, function (e) {
                    self.dispatch(event, e);
                });
            });

            /**
             * 自定義事件
             * @type {Array}
             */
            self.events = [];

            _.each(serialize.events, function (event) {
                self.addEvent(event.event, event.action, event.option);
            });

            /**
             * 是否已經(jīng)渲染
             * @type {boolean}
             */
            self.rendered = false;

            return self;
        }
    })
});
回答
編輯回答
還吻

.extend 前面的 代表require進(jìn)來(lái)的../util/helper, _init前面的_代表這是一個(gè)私有的方法,這是一種自定義的編程規(guī)范。

2018年7月5日 12:58