错误位置的等号会导致未知的令牌异常

时间:2016-02-11 16:38:25

标签: javascript reactjs babeljs

我将反应babel 5项目升级为babel 6.代码来自pluralsight github。错误发生在app.js上。我假设有一个插件需要升级才能使这种语法起作用:

export default class DriftApp extends React.Component {
  state = {//builder is not happy with this equals sign, unexpected token
    showIndex: 0,
    numSlides: 5
  }

这是哪个插件?

这是我的package.json:

 "homepage": "https://github.com/jaketrent/react-drift#readme",
  "devDependencies": {
    "autobind-decorator": "^1.3.3",
    "babel-core": "^6.5.1",
    "babel-loader": "^6.2.2",
    "babel-plugin-react-transform": "^2.0.0",
    "babel-plugin-transform-decorators-legacy": "^1.3.4",
    "babel-preset-es2015": "^6.5.0",
    "babel-preset-react": "^6.5.0",
    "express": "^4.13.3",
    "file-loader": "^0.8.4",
    "radium": "^0.16.6",
    "react": "^0.14.2",
    "react-dom": "^0.14.2",
    "react-hot-loader": "^2.0.0-alpha-4",
    "react-tools": "^0.10.0",
    "react-transform": "0.0.3",
    "react-transform-catch-errors": "^1.0.0",
    "react-transform-hmr": "^1.0.1",
    "redbox-react": "^1.1.1",
    "webpack": "^1.12.2",
    "webpack-dev-middleware": "^1.2.0",
    "webpack-hot-middleware": "^2.4.1"
  }
}

2 个答案:

答案 0 :(得分:9)

您在代码中使用属性初始值设定项语法,这是一个实验性功能(提案)。您可以通过安装babel-plugin-transform-class-properties模块

将其转换为ES5
{
  "plugins": ["transform-class-properties"]
}

将以下行添加到.babelrc文件中:

ItemTouchHelper

ref https://babeljs.io/docs/plugins/transform-class-properties/

答案 1 :(得分:2)

添加babel-preset-stage-0 npm模块并将stage-0添加到您的babel预设中。

如果您想要一个适用于React的示例babel 6配置,请查看react-redux-starter-kit中的package.json.babelrc