小编典典

Babel插件类属性– React Arrow函数

reactjs

我正在使用npm运行一个React项目。经过数小时的研究和实验,到处都有人说我必须将以下代码添加到我的“
.babelrc”文件中,该文件在我的目录中没有,因此无法创建:

{
  "plugins": [
    ["@babel/plugin-proposal-class-properties", { "loose": true }]
  ]
}

但这在运行代码时导致以下错误:

ERROR in ./src/components/NavBar/Menu.js
Module build failed (from ./node_modules/babel-loader/lib/index.js):
SyntaxError: /Users/ymoondhra/Desktop/yt-web/src/components/NavBar/Menu.js:
Support for the experimental syntax 'classProperties' isn't currently enabled
 (20:10):
  18 |     }
  19 | 
> 20 |     fast = () => {
     |          ^
  21 |         this.speed = 100;
  22 |     }
  23 | 
Add @babel/plugin-proposal-class-properties (https://git.io/vb4SL) to the 'plugins' 
section of your Babel config to enable transformation.

阅读 451

收藏
2020-07-22

共1个答案

小编典典

3小时后找到我的问题的解决方案:–在根目录中创建一个名为“ babel.config.js”的文件。–将其放入文件中。–重新运行程序(即npm
start)。

module.exports = {
  presets: [ "@babel/preset-env", "@babel/preset-react" ],
  plugins: [ "@babel/plugin-transform-arrow-functions", "@babel/plugin-proposal-    class-properties" ]
}
2020-07-22