create-react-app下的@修飾器正確的使用方式記錄


在create-react-app下使用es7的@修飾器會報錯''Support for the experimental syntax 'decorators-legacy' isn't currently enable"
原文地址https://www.jianshu.com/p/b841aee4745f

需要做以下幾步,首先正確安裝babel

"devDependencies": { "@babel/core": "^7.1.6", "@babel/plugin-proposal-class-properties": "^7.1.0", "@babel/plugin-proposal-decorators": "^7.1.6", "@babel/preset-env": "^7.1.6", } 

.babelrc文件配置:

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

在pageage.json同級目錄新建config-overrides.js並且添加內容

const { injectBabelPlugin } = require('react-app-rewired'); module.exports = function override(config, env) { // do stuff with the webpack config... config = injectBabelPlugin(['@babel/plugin-proposal-decorators', { "legacy": true }], config) //{ "legacy": true }一定不能掉,否則報錯 return config; }; 

安裝react-app-rewired並且修改啟動package.json

"scripts": { - "start": "react-scripts start", + "start": "react-app-rewired start", - "build": "react-scripts build", + "build": "react-app-rewired build", - "test": "react-scripts test", + "test": "react-app-rewired test", } 

最后運行即可解決



作者:chouchou723
鏈接:https://www.jianshu.com/p/b841aee4745f
來源:簡書
簡書著作權歸作者所有,任何形式的轉載都請聯系作者獲得授權並注明出處。


免責聲明!

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



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