Javascript 我对与巴贝尔和格尔普一起工作有何反应?
编译后我不断收到错误:Javascript 我对与巴贝尔和格尔普一起工作有何反应?,javascript,reactjs,gulp,babeljs,Javascript,Reactjs,Gulp,Babeljs,编译后我不断收到错误:找不到变量:exports 这是我的档案 Gulpfile.js var gulp = require('gulp'); var babel = require('gulp-babel'); gulp.task('react', function() { gulp.src('./resources/components/*.jsx') .pipe(babel({presets: ['react', 'es2015']})) .pip
找不到变量:exports
这是我的档案
Gulpfile.js
var gulp = require('gulp');
var babel = require('gulp-babel');
gulp.task('react', function() {
gulp.src('./resources/components/*.jsx')
.pipe(babel({presets: ['react', 'es2015']}))
.pipe(gulp.dest('./public/js'));
});
gulp.task('default', ['react']);
./resources/components/HelloWorld.jsx
import React, {PropTypes, Component} from 'react'
export default class HelloWorld extends Component {
constructor (props) {
super(props)
}
render() {
return (
<table>
<tr>
<th>Title</th>
<th>Description</th>
<th>Actions</th>
</tr>
<tr>
<td>Hello World</td>
<td>Loren ipsum</td>
<td>Delete | Update</td>
</tr>
</table>
);
}
}
ReactDOM.render(<HelloWorld/>, document.getElementById('main'));
import React,{PropTypes,Component}来自“React”
导出默认类HelloWorld扩展组件{
建造师(道具){
超级(道具)
}
render(){
返回(
标题
描述
行动
你好,世界
洛伦同侧
删除|更新
);
}
}
ReactDOM.render(,document.getElementById('main'));
index.html
<!doctype html>
<html>
<head>
<title>Crossover - Project</title>
<meta charset="utf-8">
</head>
<body>
<main id="main"></main>
<script src="./js/HelloWorld.js"></script>
</body>
</html>
交叉项目
我不知道我做错了什么。这是输出
HelloWorld.js
'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
});
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 _react = require('react');
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"); } }
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; }
var HelloWorld = function (_Component) {
_inherits(HelloWorld, _Component);
function HelloWorld(props) {
_classCallCheck(this, HelloWorld);
return _possibleConstructorReturn(this, Object.getPrototypeOf(HelloWorld).call(this, props));
}
_createClass(HelloWorld, [{
key: 'render',
value: function render() {
return _react2.default.createElement(
'table',
null,
_react2.default.createElement(
'tr',
null,
_react2.default.createElement(
'th',
null,
'Title'
),
_react2.default.createElement(
'th',
null,
'Description'
),
_react2.default.createElement(
'th',
null,
'Actions'
)
),
_react2.default.createElement(
'tr',
null,
_react2.default.createElement(
'td',
null,
'Hello World'
),
_react2.default.createElement(
'td',
null,
'Loren ipsum'
),
_react2.default.createElement(
'td',
null,
'Delete | Update'
)
)
);
}
}]);
return HelloWorld;
}(_react.Component);
// this is where the error occurs
exports.default = HelloWorld;
ReactDOM.render(_react2.default.createElement(HelloWorld, null), document.getElementById('main'));
“严格使用”;
Object.defineProperty(导出,“\uu esModule”{
值:true
});
var_createClass=function(){function defineProperties(target,props){for(var i=0;i
我对react非常陌生,我不想使用webpack,因为我已经有一个webserver+框架在运行,我想将react集成到我的应用程序中。问题是,您在HTML中包含了
HelloWorld.js
:
<script src="./js/HelloWorld.js"></script>
但浏览器不了解导出和常见js模块功能,因此不创建导出变量。此代码在浏览器中执行,而不是在node.js环境中执行
编辑:
所以,我不知道这是否是正确的地方,但我将尝试提出一些观点,以便您能够尽快跟上进度
1) ES6是一个规范,主要的浏览器供应商已经实现了其中的一部分,但不是全部,正如任何人都应该做的那样,现在,你应该使用一个transpiler来确保你的代码在每个浏览器中都能被正确解析和执行。最好的transpiler是Babel,正如你所知道的。另外,Babel可以传输JSX代码,如果你使用React,你应该使用它(注意JSX与ES6无关)
2) 由于您使用了transpiler,因此必须在每次更改时传输ES6代码以查看结果
3) Gulp是一个节点模块,是一个自动化构建系统的极好工具:观察代码的变化,在每次变化时传输代码,运行本地服务器(如您所知),以及其他许多事情
4) 您可以将webpack与gulp结合使用。尽管我建议您只使用npm脚本和webpack,但您可以顺利完成这项工作:现在将webpack集成到您现有的构建系统中,并在将来尝试使用webpack和npm脚本逐步替换您的gulp任务,因为您对webpack有了更好的了解
总结:
“[Node]、ES6、巴别塔、大口大口大口大口大口大口大口大口大口大口大口大口大口大口大口大口大口大口大口大口大口大口大口大口大口大口大口大口大