[英]Unexpected token '@' when using ES6 decorators
I have a React project setup and I'm trying to incorporate MobX into it. 我有一个React项目设置,我正在尝试将MobX合并到其中。 With that I have to use decorators ie
有了这个我必须使用装饰,即
@observable
When I do that though I get the following error: 当我这样做虽然我收到以下错误:
https://github.com/mobxjs/mobx https://github.com/mobxjs/mobx
Module build failed: SyntaxError: Unexpected token (5:22)
模块构建失败:SyntaxError:意外的令牌(5:22)
class ListStore { @observable items = ['Pete', 'John', 'Henry', 'Jeff', 'Bob'];
class ListStore {@observable items = ['Pete','John','Henry','Jeff','Bob']; }
}
My Webpack config: 我的Webpack配置:
module.exports = {
entry: './src/App.js',
output: {
path: __dirname,
filename: 'app.js'
},
module: {
loaders: [{
test: /\.jsx?$/,
exclude: /node_modules/,
loader: 'babel',
query: {
presets: ['es2015', 'react'],
plugins: ['transform-decorators-legacy']
}
},
{
test: /\.scss?$/,
exclude: /node_modules/,
loaders: ['style', 'css', 'sass']
}]
}
};
My ESLint config: 我的ESLint配置:
{
"parserOptions": {
"ecmaVersion": 6,
"ecmaFeatures": {
"jsx": true
},
"sourceType": "module"
},
"env": {
"browser": true,
"node": true,
"es6": false
},
"ecmaFeatures": {
"modules": true
},
"rules": {
"strict": [
2,
"global"
],
"quotes": [
2,
"single"
],
"indent": [
2,
4
],
"eqeqeq": [
2,
"smart"
],
"semi": [
2,
"always"
],
"max-depth": [
2,
4
],
"max-statements": [
2,
15
],
"complexity": [
2,
5
]
}
}
As a note, I'm new to using Webpack as well as utilising ESLint, so this might very well be a newby question. 作为一个注释,我是新手使用Webpack以及使用ESLint,所以这可能是一个新问题。 However after doing research online I haven't find anything that's worked.
然而,在网上进行研究后,我找不到任何有用的东西。 (This includes installing the 'transform-decorators-legacy' Babel plugin).
(这包括安装'transform-decorators-legacy'Babel插件)。
I think that the issue isn't so much the decorator, but the property initializer syntax. 我认为问题不在于装饰器,而是属性初始化器语法。 It'll probably fail on this as well:
它也可能在这方面失败:
class ListStore {
items = ['Pete', 'John', 'Henry', 'Jeff', 'Bob']
}
To support those, you can add the transform-class-properties
plugin: 要支持这些,您可以添加
transform-class-properties
插件:
$ npm install babel-plugin-transform-class-properties --save
(and update your Webpack config accordingly) (并相应地更新您的Webpack配置)
Or use a Babel preset that includes it ( stage-2
, stage-1
or stage-0
). 或者使用包含它的Babel预设(
stage-2
, stage-1
或stage-0
)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.