react裝飾器報錯的解決方法:1、透過「create-react-app mobx-study」建立專案;2、透過「yarn add -D react-app-rewired customize-cra」安裝外掛; 3.修改package.json檔案中scripts腳本;4、在專案根目錄下創建「config-overrides.js」和「.babelrc」即可。
本教學操作環境:Windows10系統、react18.0.0版、Dell G3電腦
react 裝飾器報錯怎麼辦?
React的decorators裝飾器報錯
會報錯,原因是react預設是不支援裝飾器的,所以才會報錯,所以是需要做一些配置來支援裝飾器。
【錯誤顯示:Parsing error: This experimental syntax requires enabling one of the following parser plugin(s): “decorators-legacy”, “decorators”.】npm install -g create-react-app // 安装create-react-app,已安装请忽略 create-react-app mobx-study
2. 安裝外掛程式- 改變create-react-app 中webpack 設定
yarn add -D react-app-rewired customize-cra yarn add -D @babel/core @babel/plugin-proposal-decorators @babel/preset-env
// package.json "scripts": { "start": "react-app-rewired start", "build": "react-app-rewired build", "test": "react-app-rewired test", "eject": "react-scripts eject" }
const path = require('path') const { override, addDecoratorsLegacy } = require('customize-cra') function resolve(dir) { return path.join(__dirname, dir) } const customize = () => (config, env) => { config.resolve.alias['@'] = resolve('src') if (env === 'production') { config.externals = { 'react': 'React', 'react-dom': 'ReactDOM' } } return config }; module.exports = override(addDecoratorsLegacy(), customize())
5. 在專案根目錄下建立.babelrc 並寫入以下內容 以上是react 裝飾器報錯怎麼辦的詳細內容。更多資訊請關注PHP中文網其他相關文章!{
"presets": [
"@babel/preset-env"
],
"plugins": [
[
"@babel/plugin-proposal-decorators",
{
"legacy": true
}
]
]}