Javascript 语法错误 - 当前未启用对实验性语法“decorators-legacy”的支持
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/52262084/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Syntax error - Support for the experimental syntax 'decorators-legacy' isn't currently enabled
提问by olga_babic
I'm trying to build JS react project with decorators. My .babelrc looks like this:
我正在尝试使用装饰器构建 JS react 项目。我的 .babelrc 看起来像这样:
{
"presets": [
"@babel/preset-env",
"@babel/preset-react",
],
"plugins": [
"@babel/plugin-transform-runtime",
"@babel/plugin-transform-object-assign",
[
"@babel/plugin-proposal-decorators",
{
"legacy": true
}
],
["@babel/plugin-proposal-class-properties", { "loose": true }]
]
}
Adding @babel/plugin-proposal-decorators problems appears again.
添加@babel/plugin-proposal-decorators 问题再次出现。
I am using babel 7, webpack 4 and react 16.5
我正在使用 babel 7、webpack 4 和 react 16.5
webpack.config.js:
webpack.config.js:
const path = require("path");
const webpack = require("webpack");
const componentName = "reports-desktop";
const publicFolderRelativePath = "../../../../../../public/js";
const ignorePlugin = new webpack.IgnorePlugin(/^\.\/locale$/, /moment$/);
module.exports = {
entry: './reports-desktop.js'
,
output: {
path: path.resolve(__dirname, publicFolderRelativePath),
filename: `${componentName}.js`
},
module: {
rules: [
{
test: /\.js$/,
exclude: /node_modules/,
use: {
loader: "babel-loader"
}
}
]
},
plugins: [
ignorePlugin
]
};
package.json:
包.json:
{
"name": "captain",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"watch": "webpack -w --mode development --progress --color --display-error-details",
"build": "webpack --mode production"
},
"keywords": [],
"author": "",
"license": "ISC",
"devDependencies": {
"@babel/core": "^7.0.0",
"@babel/plugin-proposal-class-properties": "^7.0.0",
"@babel/plugin-proposal-decorators": "^7.0.0",
"@babel/plugin-transform-object-assign": "^7.0.0",
"@babel/plugin-transform-runtime": "^7.0.0",
"@babel/preset-env": "^7.0.0",
"@babel/preset-react": "^7.0.0",
"@babel/preset-stage-1": "^7.0.0",
"@babel/preset-stage-2": "^7.0.0",
"babel-loader": "^8.0.2",
"babel-plugin-transform-decorators": "^6.24.1",
"react": "^16.5.0",
"react-dom": "^16.5.0",
"react-redux": "^5.0.7",
"react-router-dom": "^4.3.1",
"redux": "^4.0.0",
"webpack": "^4.17.3",
"webpack-cli": "^3.1.0"
},
"dependencies": {
"axios": "^0.18.0",
"dropzone": "^5.5.1",
"lodash": "^4.17.10",
"moment": "^2.22.2",
"prop-types": "^15.6.2",
"react-addons-update": "^15.6.2",
"react-bootstrap": "^0.32.4",
"react-datetime": "^2.15.0",
"react-dnd": "^5.0.0",
"react-dnd-html5-backend": "^5.0.1",
"react-media": "^1.8.0",
"react-tooltip": "^3.8.1"
}
}
Am I maybe using @babel/plugin-proposal-decorators wrong? As it says in documentation this should fix my problem, but it still appears.
我可能使用 @babel/plugin-proposal-decorators 错了吗?正如文档中所说,这应该可以解决我的问题,但它仍然出现。
回答by Christopher Bradshaw
I had the same problem, but I was able to get it working by running npm install --save-dev @babel/plugin-proposal-decoratorsand adding ["@babel/plugin-proposal-decorators", { "legacy": true }]to the plugins section in my .babelrc.
我遇到了同样的问题,但我能够通过运行npm install --save-dev @babel/plugin-proposal-decorators并添加["@babel/plugin-proposal-decorators", { "legacy": true }]到我的.babelrc.
The plugins section of .babelrc, for me, now looks like this:
.babelrc对我来说,插件部分现在看起来像这样:
"plugins": [
["@babel/plugin-proposal-decorators", { "legacy": true }]
]
回答by Alex Cory
Taken from mobxjs. If you're still having issues refer to this. It helped me.
取自mobxjs。如果您仍然遇到问题,请参阅此。它帮助了我。
Example config for babel 7, in decorators legacy mode:
babel 7 的示例配置,在装饰器遗留模式下:
//.babelrc
{
"presets": ["@babel/preset-env"],
"plugins": [
["@babel/plugin-proposal-decorators", { "legacy": true }],
["@babel/plugin-proposal-class-properties", { "loose": true }]
]
}
Please note that plugin ordering is important, and plugin-proposal-decorators should be the first plugin in your plugin list
请注意插件顺序很重要,plugin-proposal-decorators 应该是你插件列表中的第一个插件
"devDependencies": {
"@babel/core": "^7.1.0",
"@babel/plugin-proposal-class-properties": "^7.1.0",
"@babel/plugin-proposal-decorators": "^7.1.0",
"@babel/preset-env": "^7.1.0"
}
Non-legacy mode decorators (stage 2) is work in progress, see #1732
非传统模式装饰器(第 2 阶段)正在进行中,请参阅 #1732
Edit: updated config to show the non-beta configuration for babel 7
编辑:更新配置以显示 babel 7 的非测试版配置
回答by Abhay Kumar
If you face this error while using ReactJS with MobX, don't enable decorator syntax, but leverage the MobX's built-in decorate utility to apply decorators to your classes / objects.
如果您在将 ReactJS 与 MobX 一起使用时遇到此错误,请不要启用装饰器语法,而是利用 MobX 的内置装饰实用程序将装饰器应用于您的类/对象。
Don't:
别:
import { observable, computed, action } from "mobx";
class Timer {
@observable start = Date.now();
@observable current = Date.now();
@computed
get elapsedTime() {
return this.current - this.start + "milliseconds";
}
@action
tick() {
this.current = Date.now();
}
}
Do:
做:
import { observable, computed, action, decorate } from "mobx";
class Timer {
start = Date.now();
current = Date.now();
get elapsedTime() {
return this.current - this.start + "milliseconds";
}
tick() {
this.current = Date.now();
}
}
decorate(Timer, {
start: observable,
current: observable,
elapsedTime: computed,
tick: action
});
回答by ulyC
First, execute the command:
首先,执行命令:
npm install customize-cra react-app-rewired @babel/plugin-proposal-decorators --save
npm install customize-cra react-app-rewired @babel/plugin-proposal-decorators --save
Create a new file config-overrides.jsat project root and then execute the following:
config-overrides.js在项目根目录创建一个新文件,然后执行以下命令:
const { override, addDecoratorsLegacy } = require('customize-cra');
module.exports = override(
addDecoratorsLegacy()
);
Also, edit your package.jsonfile :
另外,编辑你的package.json文件:
"scripts": {
"start": "react-app-rewired start",
"build": "react-app-rewired build",
"test": "react-app-rewired test",
"eject": "react-app-rewired eject"
},
then restart.
然后重新启动。
回答by wuliwong
I fixed this with yarn add @babel/plugin-proposal-decorators
我用 yarn add @babel/plugin-proposal-decorators
Then I added the following to babel.config.jsin the pluginssection:
然后我babel.config.js在该plugins部分添加了以下内容:
[
require('@babel/plugin-proposal-decorators').default,
{
legacy: true
}
],
Finally I needed to restart my webpack dev server.
最后我需要重新启动我的 webpack 开发服务器。
I haven't tested this but like @christopher bradshaw answers says and according to the babel website if you are using .babelrcfor configuration then add the following to the "plugins"section:
我还没有测试过这个,但就像@christopher bradshaw 回答说的那样,根据 babel 网站,如果您.babelrc用于配置,则将以下内容添加到该"plugins"部分:
["@babel/plugin-proposal-decorators", { "legacy": true }]
回答by VahidN
Unfortunately none of the mentioned solutions worked for me. Because they need you to run npm run ejectfirst and ... I don't want to do that. To change and override the webpack's configs at runtime, there's a package called react-app-rewiredand this is how it should be used:
不幸的是,上述解决方案都不适用于我。因为他们需要你先跑npm run eject……我不想那样做。要在运行时更改和覆盖 webpack 的配置,有一个包被调用react-app-rewired,这就是它的使用方式:
First install the required dependencies:
首先安装所需的依赖项:
npm i --save-dev customize-cra react-app-rewired
Then add a new file called config-overrides.jsto the root folder of the project with this content to enable legacy decorators babel plugin:
然后config-overrides.js在项目的根文件夹中添加一个新文件,该文件包含此内容以启用旧式装饰器 babel 插件:
const {
override,
addDecoratorsLegacy,
disableEsLint
} = require("customize-cra");
module.exports = override(
// enable legacy decorators babel plugin
addDecoratorsLegacy(),
// disable eslint in webpack
disableEsLint()
);
Finally change the package.jsonfile to use react-app-rewired:
最后更改package.json要使用的文件react-app-rewired:
"scripts": {
"start": "react-app-rewired start",
"build": "react-app-rewired build",
"test": "react-app-rewired test",
"eject": "react-app-rewired eject"
},
Now run the npm startas usual and enjoy it!
现在npm start像往常一样运行并享受它!
回答by zloctb
package.json
包.json
"@babel/core": "^7.2.2",
"@babel/plugin-proposal-class-properties": "^7.1.0",
"@babel/plugin-proposal-decorators": "^7.1.0",
"@babel/preset-env": "^7.2.3",
"@babel/preset-react": "^7.0.0",
"@babel/register": "^7.0.0",
"babel-loader": "^8.0.5"
webpack.config.js
webpack.config.js
presets: ["@babel/preset-env", "@babel/preset-react"]
.babelrc
.babelrc
{
"presets": [
"@babel/preset-env",
"@babel/preset-react"
],
"plugins": [
["@babel/plugin-proposal-decorators", { "legacy": true }],
["@babel/plugin-proposal-class-properties", { "loose": true }]
]
}
回答by Anton Matiashevich
Renamed .babelrcto babel.config.jsonand it worked!!!
重命名.babelrc为babel.config.json,它起作用了!!!
回答by Ati Barzideh
I tried to install babel-plugin-transform-inline-environment-variablesand it worked.
我尝试安装babel-plugin-transform-inline-environment-variables并且成功了。
npm install babel-plugin-transform-inline-environment-variables

