语法错误 - 目前尚未启用对实验语法'decorators-legacy'的支持

olg*_*bic 29 javascript babel

我正在尝试用装饰器构建JS反应项目.我的.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 }]
  ]
}
Run Code Online (Sandbox Code Playgroud)

添加@ babel/plugin-proposal-decorators问题再次出现.

我使用babel 7,webpack 4并做出反应16.5

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
    ]
};
Run Code Online (Sandbox Code Playgroud)

的package.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"
  }
}
Run Code Online (Sandbox Code Playgroud)

我可能使用@ babel/plugin-proposal-decorators错了吗?正如它在文档中所说,这应该解决我的问题,但它仍然出现.

Chr*_*haw 36

我有同样的问题,但我能够通过运行npm install --save-dev @babel/plugin-proposal-decorators并添加["@babel/plugin-proposal-decorators", { "legacy": true }]到我的插件部分来使其工作.babelrc.

.babelrc对我来说,插件部分现在看起来像这样:

"plugins": [
  ["@babel/plugin-proposal-decorators", { "legacy": true }]
]
Run Code Online (Sandbox Code Playgroud)


wul*_*ong 12

我用 yarn add @babel/plugin-proposal-decorators

然后我babel.config.js在该plugins部分添加了以下内容:

  [
    require('@babel/plugin-proposal-decorators').default,
    {
      legacy: true
    }
  ],
Run Code Online (Sandbox Code Playgroud)

最后我需要重新启动我的 webpack 开发服务器。


我还没有测试过这个,但就像@christopher bradshaw 回答说的那样,根据 babel 网站,如果您.babelrc用于配置,则将以下内容添加到该"plugins"部分:

  ["@babel/plugin-proposal-decorators", { "legacy": true }]
Run Code Online (Sandbox Code Playgroud)


Ale*_*ory 8

取自mobxjs。如果仍然有问题,请参考。它帮助了我。

在装饰器旧版模式下babel 7的示例配置:

//.babelrc

{
    "presets": ["@babel/preset-env"],
    "plugins": [
        ["@babel/plugin-proposal-decorators", { "legacy": true }],
        ["@babel/plugin-proposal-class-properties", { "loose": true }]
    ]
}
Run Code Online (Sandbox Code Playgroud)

请注意,插件排序很重要,并且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"
}
Run Code Online (Sandbox Code Playgroud)

非传统模式装饰器(阶段2)正在开发中,请参阅#1732

编辑:更新了配置以显示babel 7的非beta配置


Abh*_*mar 7

如果在将ReactJS与MobX一起使用时遇到此错误,

Don't enable decorator syntax, but leverage the MobX built-in utility decorate to apply decorators to your classes / objects.

别:

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();
  }
}
Run Code Online (Sandbox Code Playgroud)

做:

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
});
Run Code Online (Sandbox Code Playgroud)

  • 这是一种解决方法,而不是问题的实际解决方案。选择不使用装饰器是合法的,但如果您决定在代码中使用装饰器语法,则应该设置配置以使用它们,而不是放弃,因为所需的神奇 babel 咒语太晦涩了。 (12认同)

uly*_*lyC 5

首先,执行命令:

npm install customize-cra react-app-rewired @babel/plugin-proposal-decorators --save

config-overrides.js在项目根目录下创建一个新文件,然后执行以下操作:

const { override, addDecoratorsLegacy } = require('customize-cra');
module.exports = override(
 addDecoratorsLegacy()
 );
Run Code Online (Sandbox Code Playgroud)

另外,编辑package.json文件吗?

"scripts": {
 "start": "react-app-rewired start",
 "build": "react-app-rewired build",
 "test": "react-app-rewired test",
 "eject": "react-app-rewired eject"
  },
Run Code Online (Sandbox Code Playgroud)

然后重新启动。


小智 5

  1. 在 package.json 中安装装饰器
"@babel/plugin-proposal-decorators": "^7.3.0",
"@babel/preset-flow": "^7.0.0"
Run Code Online (Sandbox Code Playgroud)
  1. 用这个更新 babe.config.js
module.exports = {
  "presets": [
    "module:metro-react-native-babel-preset",
    "@babel/preset-flow"
  ],
  "plugins": [
    ["@babel/plugin-proposal-decorators", { "legacy" : true }],
    ["@babel/plugin-proposal-class-properties", { "loose" : true }],
  ]
}
Run Code Online (Sandbox Code Playgroud)