解決react項目:Support for the experimental syntax 'decorators-legacy' isn't currently enabled


react項目中有用到高階組件裝飾器的寫法,然后項目就報錯,信息如下:

Support for the experimental syntax 'decorators-legacy' isn't currently enabled

解決方法:

1、安裝 babel-plugin-transform-decorators-legacy

yarn add babel-plugin-transform-decorators-legacy

2、修改配置文件,package.json 中加入以下代碼:

"plugins": [

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

此時 package.json 里面還沒有 babel 配置,需要執行

yarn eject

把沒有展示的react-script的配置都展示在 package.json 中

如果執行完 發現控制台報錯,解決方法就是把本地的修改提交了,執行

git add
git commit -m ''

然后執行 yarn eject 

3、安裝 babel-plugin-transform-decorators-legacy

4、修改 package.json 中的 babel

"babel": {

"plugins": [

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

],

"presets": [

"react-app"

]}

參考:http://metronic.net.cn/metronic/show-51455.html


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM