简体   繁体   中英

How to Enable Private Method Syntax Proposal in React App?

I got "Class private methods are not enabled." error when running npm start on a project using leading # to indicate private methods. I followed this answer: https://stackoverflow.com/a/55822103/4258041 to enable the decorator and it worked, but I cannot find corresponding customize-cra components to add private method syntax in a same way. "@babel/plugin-proposal-private-methods": "^7.14.5" is already installed and saved in my packages.json.

You can use the @babel/plugin-proposal-class-properties. Install with

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

then add it to your.babelrc plugin section:

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

Please consider that while I'm writing this is already an outdated answer, since Class Fields are no longer a proposal since ES2022 and this plugin is included in @babel/preset-env.

See here for further information.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM