creat-react-app Ver2.0+配置裝飾器語法

1、eject 項目釋放自定義配置文件

執行 yarn eject | npm run eject

2、安裝 babel 相關依賴

執行 yarn add @babel/plugin-proposal-decorators

3、配置 package.json

3.1 配置 babel 插件 --> 使項目支持@裝飾器
"babel": {
    "presets": [
      "react-app"
    ],
    "plugins": [
      [
        "@babel/plugin-proposal-decorators",
        {
          "legacy": true
        }
      ]
    ]
  }
3.2 配置 eslintConfig ---> 使 export 支持放在@裝飾器之后
"eslintConfig": {
    "extends": "react-app",
    "parserOptions": {
      "ecmaFeatures": {
        "legacyDecorators": true
      }
    }
  }

//配置 eslintConfig 前用法

@decorators
class Com extends Components {
  // your code
}
export default Com;

或

export default
@decorators
class Com extends Components {
    // your code
}

//修改后

@decorators
export default class Com extends Components {
    // your code
}

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。