尝试在我的React项目中使用装饰器,即使我已经安装了启用它们的babel插件,并引用了.babelrc
中的插件,我仍然在控制台中出现语法错误。
的package.json
// ...
"devDependencies": {
"babel-plugin-transform-decorators-legacy": "^1.3.4",
"babel-plugin-transform-object-rest-spread": "^6.16.0",
"chai": "^3.5.0",
"file-loader": "^0.9.0",
"url-loader": "^0.5.7"
}
// ...
.babelrc - 我之前安装了transform-object-rest-spread
插件,所以刚刚扩展了plugins
数组:
{
"plugins": ["transform-object-rest-spread", "transform-decorators-legacy"]
}
控制台错误,完整(以防万一):
BabelLoaderError: SyntaxError: Decorators are not officially supported yet in 6.x pending a proposal update.
However, if you need to use them you can install the legacy decorators transform with:
npm install babel-plugin-transform-decorators-legacy --save-dev
and add the following line to your .babelrc file:
{
"plugins": ["transform-decorators-legacy"]
}
The repo url is: https://github.com/loganfsmyth/babel-plugin-transform-decorators-legacy.
我错过了什么?
答案 0 :(得分:0)
我已将插件包含在.babelrc
中但不包含webpack.config.js
(如azium指出的那样)。
当我加入时,它起作用了:
// ...
query: {
plugins: [ 'transform-decorators-legacy' ]
}
// ...
希望有助于以防其他人遇到此问题:)