React-MobX错误:“ decorators”插件需要一个“ decoratorsBeforeExport”选项,其值必须为布尔值

时间:2018-11-09 17:50:53

标签: reactjs decorator mobx-react

我收到以下错误消息:如果您要从Babylon / Babel 6迁移或想使用旧的装饰器提案,则应使用“ decorators-legacy”插件而不是“ decorators”。

package.json

"@babel/plugin-proposal-decorators": {
      "version": "7.1.2",
      "resolved": "https://registry.npmjs.org/@babel/plugin-proposal-decorators/-/plugin-proposal-decorators-7.1.2.tgz",
      "integrity": "sha512-YooynBO6PmBgHvAd0fl5e5Tq/a0pEC6RqF62ouafme8FzdIVH41Mz/u1dn8fFVm4jzEJ+g/MsOxouwybJPuP8Q==",
      "requires": {
        "@babel/helper-plugin-utils": "^7.0.0",
        "@babel/helper-replace-supers": "^7.1.0",
        "@babel/helper-split-export-declaration": "^7.0.0",
        "@babel/plugin-syntax-decorators": "^7.1.0"
      }
    },

 "@babel/plugin-syntax-decorators": {
      "version": "7.1.0",
      "resolved": "https://registry.npmjs.org/@babel/plugin-syntax-decorators/-/plugin-syntax-decorators-7.1.0.tgz",
      "integrity": "sha512-uQvRSbgQ0nQg3jsmIixXXDCgSpkBolJ9X7NYThMKCcjvE8dN2uWJUzTUNNAeuKOjARTd+wUQV0ztXpgunZYKzQ==",
      "requires": {
        "@babel/helper-plugin-utils": "^7.0.0"
      }
    },

"babel-plugin-syntax-decorators": {
      "version": "6.13.0",
      "resolved": "http://registry.npmjs.org/babel-plugin-syntax-decorators/-/babel-plugin-syntax-decorators-6.13.0.tgz",
      "integrity": "sha1-MSVjtNvePMgGzuPkFszurd0RrAs=",
      "dev": true
    },
    "babel-plugin-transform-decorators-legacy": {
      "version": "1.3.5",
      "resolved": "https://registry.npmjs.org/babel-plugin-transform-decorators-legacy/-/babel-plugin-transform-decorators-legacy-1.3.5.tgz",
      "integrity": "sha512-jYHwjzRXRelYQ1uGm353zNzf3QmtdCfvJbuYTZ4gKveK7M9H1fs3a5AKdY1JUDl0z97E30ukORW1dzhWvsabtA==",
      "dev": true,
      "requires": {
        "babel-plugin-syntax-decorators": "^6.1.18",
        "babel-runtime": "^6.2.0",
        "babel-template": "^6.3.0"
      }
    },
"requires": {
     "@babel/plugin-proposal-decorators": "7.1.2",
}

tsconfig.json

{
    "compilerOptions": {
        "experimentalDecorators": true,
        "allowJs": true
    }
}

5 个答案:

答案 0 :(得分:10)

该错误消息有些令人困惑,但是通过进行一些深入的搜索,您可以使用以下方法解决该问题。

除了您使用本指南中的webpack之外,我不做任何假设。

您需要将babel提案装饰器添加到您的开发依赖项中(您仅在开发期间需要它)(已经添加了)。

如果使用纱线

yarn add --dev @babel/plugin-proposal-decorators 

为npm设置其他

npm install --save-dev @babel/plugin-proposal-decorators 

然后在您的package.json文件中,找到babel config部分,如果不存在则添加一个。配置名称严格是“ babel”。

  "babel": {
    "presets": [
      "react-app"
    ],
    "plugins": [
      [
        "@babel/plugin-proposal-decorators",
        {
          "legacy": true
        }
      ]
    ]
  }

如果手动输入缩进,请格外注意。请注意,对象"@babel/plugin-proposal-decorators"深深地嵌套在两个数组中,因此必须这样工作。

出于完整性检查的考虑,您的devDependencies至少应为

  "devDependencies": {
    "@babel/plugin-proposal-decorators": "^7.1.2"
  }

现在,您可以使用yarn或npm构建应用程序,并从此过上幸福的生活。

答案 1 :(得分:1)

{
 "presets": ['@babel/preset-env', '@babel/preset-react'],
 "plugins": [
  ["import", { "libraryName": "antd", "libraryDirectory": "es", "style": "less" }],
  [
    "@babel/plugin-proposal-decorators",
    {
      "decoratorsBeforeExport":true
    }
  ]
 ]
}

答案 2 :(得分:1)

反应原生0.59

babel.config.js:

{ 
  "presets": ["module:metro-react-native-babel-preset"],
  "plugins": [
        ["@babel/plugin-transform-flow-strip-types"],
        ["@babel/plugin-proposal-decorators", { "legacy": true}],
        ["@babel/plugin-proposal-class-properties", { "loose": true}]
    ]
}

npm install @babel/plugin-transform-flow-strip-types @babel/plugin-proposal-decorators @babel/plugin-proposal-class-properties --save

来源:https://github.com/facebook/react-native/issues/20588#issuecomment-448218111

答案 3 :(得分:0)

"babel": {
   "presets": [
     "react-app"
   ],
  "plugins": [
  [
    "@babel/plugin-proposal-decorators",
    {
      "legacy": true
    }
  ],
  [
    "@babel/plugin-proposal-class-properties",
    {
      "loose": true
    }
   ]
  ]
 },
 "devDependencies": {
 "@babel/plugin-proposal-decorators": "^7.3.0"
}

答案 4 :(得分:0)

答案在正式文件中: https://mobx.js.org/best/decorators.html

您可以在“启用装饰器语法”部分找到许多启用它的方法。

以Babel 7为例,从头开始使用mobx + create-react-app创建一个项目:

npx create-react-app hello-mobx

//This moves files around and makes your app’s configuration accessible.
npm run eject

npm install --save-dev babel-plugin-transform-decorators-legacy
npm install --save-dev @babel/plugin-proposal-decorators
npm install --save-dev @babel/plugin-proposal-class-properties

编辑package.json: package.json:

"babel": {
  "plugins":[
    [
      "@babel/plugin-proposal-decorators",
      {
        "legacy":true
      }
    ],
    [
      "@babel/plugin-proposal-class-properties",
      {
        "loose":true
      }
    ]
  ],
  "presets":[
    "react-app"
  ]
}

安装mobx:

npm install mobx --save
npm install mobx-react --save

享受!