1

我的组件代码如下所示。

    import React, { Component, PropTypes } from 'react';

export default class HeroBanner extends Component {
  constructor(props) {
    super(props);
  }

  render() {
    const { columns, content } = this.props.data;

    return (
      <div className="conatainer-class">

      </div>
    );
  }
}

HeroBanner.propTypes = {
  content: PropTypes.object,
  columns: PropTypes.string,
};

我的 .babelrc 文件是:

{
 "presets": ["react", "es2015","stage-0"],
 "plugins": ["transform-es2015-modules-amd"]
}

当编译发生时,它以“对象”而不是函数或字符串的形式给出输出。

这是输出对象。Banner: Banner(props) esModule: true __proto : Object

这导致 React 抛出错误:

Uncaught Invariant Violation:元素类型无效:期望字符串(对于内置组件)或类/函数(对于复合组件)但得到:对象。

转译代码:

    'use strict';

var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol ? "symbol" : typeof obj; };

define(['exports', 'react'], function (exports, _react) {
  Object.defineProperty(exports, "__esModule", {
    value: true
  });

  var _react2 = _interopRequireDefault(_react);

  function _interopRequireDefault(obj) {
    return obj && obj.__esModule ? obj : {
      default: obj
    };
  }

  function _classCallCheck(instance, Constructor) {
    if (!(instance instanceof Constructor)) {
      throw new TypeError("Cannot call a class as a function");
    }
  }

  var _createClass = function () {
    function defineProperties(target, props) {
      for (var i = 0; i < props.length; i++) {
        var descriptor = props[i];
        descriptor.enumerable = descriptor.enumerable || false;
        descriptor.configurable = true;
        if ("value" in descriptor) descriptor.writable = true;
        Object.defineProperty(target, descriptor.key, descriptor);
      }
    }

    return function (Constructor, protoProps, staticProps) {
      if (protoProps) defineProperties(Constructor.prototype, protoProps);
      if (staticProps) defineProperties(Constructor, staticProps);
      return Constructor;
    };
  }();

  function _possibleConstructorReturn(self, call) {
    if (!self) {
      throw new ReferenceError("this hasn't been initialised - super() hasn't been called");
    }

    return call && ((typeof call === 'undefined' ? 'undefined' : _typeof(call)) === "object" || typeof call === "function") ? call : self;
  }

  function _inherits(subClass, superClass) {
    if (typeof superClass !== "function" && superClass !== null) {
      throw new TypeError("Super expression must either be null or a function, not " + typeof superClass);
    }

    subClass.prototype = Object.create(superClass && superClass.prototype, {
      constructor: {
        value: subClass,
        enumerable: false,
        writable: true,
        configurable: true
      }
    });
    if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass;
  }

  var HeroBanner = function (_Component) {
    _inherits(HeroBanner, _Component);

    function HeroBanner(props) {
      _classCallCheck(this, HeroBanner);

      var _this = _possibleConstructorReturn(this, Object.getPrototypeOf(HeroBanner).call(this, props));

      _this.createMarkup = _this.createMarkup.bind(_this);
      return _this;
    }

    _createClass(HeroBanner, [{
      key: 'createMarkup',
      value: function createMarkup(val) {
        return {
          __html: val
        };
      }
    }, {
      key: 'render',
      value: function render() {
        var _this2 = this;

        var _props$data = this.props.data;
        var columns = _props$data.columns;
        var content = _props$data.content;
        var containerClass = 'l-col-xs-' + columns.toString() + ' l-col-nopadding' + ' h-border-bottom';
        var storyCardClass = 'storycard storycard-' + content.text_position.toString();
        return _react2.default.createElement('div', {
          className: containerClass
        }, function () {
          if (content.headline_url) {
            return _react2.default.createElement('a', {
              className: storyCardClass,
              href: content.headline_url
            }, content.image && _react2.default.createElement('img', {
              className: 'storycard--image',
              src: content.image.image_path,
              alt: content.image.image_alt_text
            }), _react2.default.createElement('div', {
              className: 'storycard--text'
            }, content.headline && _react2.default.createElement('div', {
              className: 'storycard--headline',
              dangerouslySetInnerHTML: _this2.createMarkup(content.headline)
            }), content.subhead && _react2.default.createElement('div', {
              className: 'storycard--detail',
              dangerouslySetInnerHTML: _this2.createMarkup(content.subhead)
            })));
          }

          return _react2.default.createElement('div', {
            className: 'storycard storycard-<%= contents.text_position %>'
          });
        }());
      }
    }]);

    return HeroBanner;
  }(_react.Component);

  exports.default = HeroBanner;
  HeroBanner.propTypes = {
    content: _react.PropTypes.object,
    columns: _react.PropTypes.string
  };
});

任何帮助将不胜感激。让我知道是否需要更多详细信息!

4

1 回答 1

2

如果你

export default

ES6 模块中的某些内容,然后在 ES5 模块中您需要通过以下方式访问它.default

var HeroBanner = require("HeroBannerModule").default;

或类似的,在 RequireJS 中,

HeroBanner = HeroBanner.default

有关更多信息,请参阅误解 ES6 模块、升级 Babel、Tears 和解决方案,如果您真的想恢复旧行为,请参阅add-module-exports 插件。

于 2016-01-12T22:51:22.700 回答