Babel插件类属性– React Arrow函数 [英] Babel Plugin Class Properties – React Arrow Functions

查看:281
本文介绍了Babel插件类属性– React Arrow函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am running a React project using npm. After hours of research and experimenting, everywhere says I have to add the following code to my ".babelrc" file, which I do not have in my directory and cannot create:

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

但是当我运行代码时,这会导致以下错误:

But this leads to the following error when I run the code:

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.

推荐答案

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

Found the solution to my question after 3 hours: –create a file called "babel.config.js" in the root directory. –put this in the file. –rerun the program (i.e. npm start).

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

这篇关于Babel插件类属性– React Arrow函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆