微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

javascript – 在WebStorm / PhpStorm中,使用Babel文件观察程序时未定义错误

我一直在关注这个guide,将ES6代码转换PHPStorm中的ES5代码.

我的问题是当脚本有一个import语句时:

import TestingView from 'component/TestingView';
class ItemPage extends React.Component {
    render () {
        return <TestingView>
    }
}

TestingView.js

export default class TestingView extends React.Component{
    render(){
        return <div>Hello!!!!</div>
    }
}

文件监视器将其转换为以下形式:

'use strict';

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; }; }();

var _TestingView = require('component/TestingView');

var _TestingView2 = _interoprequiredefault(_TestingView);

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"); } }

function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (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; } /**
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                * Created by laukaichung on 7/10/16.
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                */

var ItemPage = function (_React$Component) {
    _inherits(ItemPage, _React$Component);

    function ItemPage() {
        _classCallCheck(this, ItemPage);

        return _possibleConstructorReturn(this, Object.getPrototypeOf(ItemPage).apply(this, arguments));
    }

    _createClass(ItemPage, [{
        key: 'render',
        value: function render() {

            return React.createElement(_TestingView2.default, null);
        }
    }]);

    return ItemPage;
}(React.Component);
//# sourceMappingURL=item_page.js.map

显然,构建的脚本不能在客户端工作,因为有一个必需的语句.它会抛出我未捕获的ReferenceError:require未定义.

有没有解决方案在工作脚本中编译它?我知道browserify但我找不到任何关于它如何被集成到babel文件观察器的信息.

观察者配置:

Program : /home/something/node_modules/.bin/babel
Arguments :$FileDir$--source-maps --out-dir /something/js/build
Working directory: $ProjectFileDir$

解决方法:

请参阅require is not defined:您正在将ES6模块编译为Commonjs格式,因此问题.您需要使用browserify或WebPack捆绑您的模块,或者编译为AMD格式(transform-es2015-modules-amd)并在yoiur应用程序中包含Require.js

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐