3

我正在尝试使用 babel 编译以下代码,但它在下一行的字符“=”上给了我一个“意外标记”:

状态 = {};

Babel 版本为:6.24.1(babel-core 6.25.0)

编码:

import {Button, Menu} from 'semantic-ui-react';

class AppBar extends Component {
    state = {};

    handleItemClick = (e, { name }) => this.setState({ activeItem: name });

    render() {
        const { activeItem } = this.state;

        return (
            <Menu>
                <Menu.Item
                    name='dashboard'
                    active={activeItem === 'dashboard'}
                    onClick={this.handleItemClick}
                >
                    Dashboard
                </Menu.Item>

                <Menu.Item
                    name='contacts'
                    active={activeItem === 'contacts'}
                    onClick={this.handleItemClick}
                >
                    Contact Lists
                </Menu.Item>

                <Menu.Item
                    name='messages'
                    active={activeItem === 'messages'}
                    onClick={this.handleItemClick}
                >
                    Messages
                </Menu.Item>
            </Menu>
        )
    }
}

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

我正在使用 es2015 并做出反应预设。

问题是什么?谢谢。

4

3 回答 3

3

将包添加babel-plugin-transform-class-properties到您的项目中。

于 2017-07-13T14:13:15.013 回答
3

我目前正在package.json为我们的 React 应用程序使用这些 babel 模块(在 中):

"babel-cli": "~6.24.1",
"babel-core": "~6.25.0",
"babel-eslint": "~7.2.3",
"babel-loader": "~7.1.0",
"babel-plugin-add-module-exports": "~0.2.1",
"babel-plugin-istanbul": "^4.1.3",
"babel-plugin-syntax-dynamic-import": "^6.18.0",
"babel-plugin-transform-class-properties": "~6.24.1",
"babel-preset-es2015": "~6.24.1",
"babel-preset-react": "~6.24.1",
"babel-preset-stage-3": "^6.24.1",
"babel-register": "~6.24.1",
"babel-runtime": "~6.23.0",

.bablerc在根文件夹中有以下内容:

{
  "presets": [
    "es2015", "react", "stage-3"
  ],
  "plugins": ["transform-class-properties", "add-module-exports","syntax-dynamic-import"],
  "env": {
    "development": {
      "plugins": ["react-hot-loader/babel"]
    },
    "coverage": {
      "plugins": ["istanbul"]
    }
  }
}

state使用相同的代码定义

export class AttributeModelListContainer extends React.Component {

  static propTypes = {
    // from router
    params: PropTypes.shape({
      project: PropTypes.string,
    }),
    // from mapStateToProps
    [...]
  }

  state = {
    isLoading: true,
  }
}
于 2017-07-13T14:14:37.470 回答
0

确保你已经安装了所有你需要的 babel 预设:

yarn add babel-loader babel-core --dev
yarn add babel-polyfill --dev
yarn add babel-preset-es2015 --dev
yarn add babel-preset-stage-2 --dev
yarn add babel-preset-react --dev

babel 按以下顺序预设为 .babelrc:

{
  "presets": ["es2015", "react", "stage-2"]
}

包括第 2 阶段应该有足够的新功能,您需要为您的开发。

于 2017-07-13T14:27:35.973 回答