gpt4 book ai didi

javascript - 如何使用 babel 编译我用 es6 编写的 react 文件?

转载 作者:行者123 更新时间:2023-11-30 14:45:11 24 4
gpt4 key购买 nike

你能告诉我如何使用 babel 编译我用 es6 编写的 react 文件吗?

我试过这样我的文件

import React from 'react';
import { render } from 'react-dom';


class Abc extends React.Components{

render(){
return (
<div>dddd</div>
)
}
}

render(<Abc />, document.getElementById('root'));

package.json

包.json

{
"name": "ifscproject",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"build": "babel reacfiles/app.js --presets react --out-file build/app.js"
},
"author": "",
"license": "ISC",
"devDependencies": {
"axios": "^0.18.0",
"babel-cli": "^6.26.0",
"babel-core": "^6.26.0",
"babel-loader": "^7.1.3",
"babel-preset-env": "^1.6.1",
"babel-preset-react": "^6.24.1",
"express": "^4.16.2",
"react": "^16.2.0",
"react-dom": "^16.2.0"
}
}

当我尝试npm run build

它生成一个新文件

import React from 'react';
import { render } from 'react-dom';

class Abc extends React.Components {

render() {
return React.createElement(
'div',
null,
'dddd'
);
}
}

render(React.createElement(Abc, null), document.getElementById('root'));

它是转换的react代码,而不是es6代码为什么?你能告诉我如何转换 es6 代码吗

更新

当我使用解决方案时,它会提供更新代码

'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 _react = require('react');

var _react2 = _interopRequireDefault(_react);

var _reactDom = require('react-dom');

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 Abc = function (_React$Components) {
_inherits(Abc, _React$Components);

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

return _possibleConstructorReturn(this, (Abc.__proto__ || Object.getPrototypeOf(Abc)).apply(this, arguments));
}

_createClass(Abc, [{
key: 'render',
value: function render() {
return _react2.default.createElement(
'div',
null,
'dddd'
);
}
}]);

return Abc;
}(_react2.default.Components);

(0, _reactDom.render)(_react2.default.createElement(Abc, null), document.getElementById('root'));

但是当我在服务器上运行时它给我错误

server.js

var express = require('express');
var React = require('react');
var ReactDOMServer = require('react-dom/server');
var App = require('./build/app');

var React = require('react');
var path = require('path');
var app = express();
// const indexPath = path.join(__dirname, 'index.html');
const publicPath = express.static(path.join(__dirname, './public'));
app.use(publicPath);
const port = process.env.PORT || 3000;


app.get('/get', function (req, res) {
res.send(App);
})
app.listen(port, function () {
console.log('Listening to port 3000');
})

最佳答案

您缺少 es2015 预设。执行以下操作:

  • 安装 es2015 预设

    npm install --save-dev babel-preset-es2015
  • 将您的构建脚本编辑为:

    "build": "babel reacfiles/app.js --out-file build/app.js"
  • 创建一个 .babelrc 文件(并在其中定义您的预设):

.babelrc

{ "presets": ["es2015", "react"] }

关于javascript - 如何使用 babel 编译我用 es6 编写的 react 文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49048579/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com