我正在使用Babel和React js。问题是我不想把es6转换成es5。我想继续使用es6。我只需要把jsx转换成js。这是我的.babelrc
{
"plugins": ["transform-react-jsx"]
}这是我的代码:
import React from "react";
/****** Header *******/
export class Header extends React.Component {
onSubmit = (e) => {
e.preventDefault();
console.log('Submitting');
const errors = this.validate();
if (Object.keys(errors).length === 0) {
this.setState({ loading: true });
fetch(this.props.action, {
method: 'POST',
body: JSON.stringify(this.state.data),
headers: new Headers({
'Content-Type': 'application/json'
}),
credentials: 'same-origin'
}).then(res => res.json())
.catch(error => console.error('Error:', error))
.then(response => console.log('Success:', response));
}
};
render() {
return (
<div>
<a href={this.props.homeUrl}><img src={this.props.logoUrl} /></a>
<div><span>Hello {this.props.userName}</span></div>
<button onclick={this.onSubmit(e).bind(this)}>Click me</button>
</div>
);
}
}这是我运行"babel components/Header.js -o Header.babel.js“时看到的内容。
SyntaxError: components/Header.js: Unexpected token (5:13)
3 | /****** Header *******/
4 | export class Header extends React.Component {
> 5 | onSubmit = (e) => {
| ^
6 | e.preventDefault();
7 | console.log('Submitting');
8 | const errors = this.validate();它显示了es6代码行的语法错误,因此我认为Babel仍然需要将es6转换为es5
在我安装了"transform-class-properties“之后,它显示了另一条错误消息:
SyntaxError: components/Form.js: Unexpected token (55:12)
53 | getFieldValue(e) {
54 | this.setState({
> 55 | ...this.state,
| ^
56 | data: {
57 | ...this.state.data,
58 | [e.target.name]: e.target.value你知道有什么方法可以做到这一点吗?仅将JSX转换为JS
非常感谢您的支持
发布于 2018-03-02 13:58:24
您正在使用类属性语法,例如someVar = () => {}
它不会被jsx插件转换。您需要为所有这些仍在提议中的高级功能添加插件。
下面是类属性的babel插件:https://babeljs.io/docs/plugins/transform-class-properties/
本质上,它只会转换你给它的插件,但是浏览器还不支持类属性,更不用说node了。
发布于 2018-03-02 13:44:54
这取决于您使用的浏览器。如果该浏览器不支持所有es6功能或部分功能,则这些错误是令人担忧的
https://stackoverflow.com/questions/49062293
复制相似问题