简体   繁体   English

Babel-cli类属性不适用于Stage-2

[英]Babel-cli class properties are not working with stage-2

I have this code where I am using class properties for react component state. 我在使用类属性来反应组件状态的地方有这段代码。

class MyComponent extends React.Component {

        state = {
            filter: '',
            searchTerm: ''
        }
    }

This code is working fine in babel-repl when I have selected presets react,stage-2. 当我选择了预设react,stage-2时,此代码在babel-repl中工作正常。 But it doesn't work when I try to transpile using babel-cli. 但是,当我尝试使用babel-cli进行转译时,它不起作用。 My package.json have - 我的package.json有-

"scripts": {
  "build": "babel src -d lib"
},
"babel-cli": "^6.14.0",
"babel-plugin-transform-object-assign": "^6.8.0",
"babel-preset-latest": "^6.14.0",
"babel-preset-react": "^6.11.1",
"babel-preset-stage-2": "^6.13.0"

And when I run npm run build I keep getting this error and it particularly highlights equality operator after state variable. 当我运行npm run build我一直收到此错误,并且它特别突出显示状态变量之后的等号运算符。

SyntaxError: react/Fidelis.K2.Views.React.BrowseCommunities.js: 语法错误:react / Fidelis.K2.Views.React.BrowseCommunities.js:

Unexpected token (2:14) 意外令牌(2:14)

1 | 1 | class MyComponent extends React.Component { 类MyComponent扩展了React.Component {
2 | 2 | state = { 状态= {

3 | 3 | filter: '', 过滤器:“,
4 | 4 | searchTerm: '' 搜索词: ''
5 | 5 | } }

Found it. 找到了。 The required preset wasn't mentioned in .babelrc .babelrc中未提及所需的预设

Once I updated it, this was working fine. 一旦更新,它就可以正常工作。 Here is my updated .babelrc file 这是我更新的.babelrc文件

{
  "presets":  ["latest", "react", "stage-2"],
  "plugins": [
    "transform-object-assign"
  ]
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM