2.1启用装饰器语法支持(方式一)

    1.弹射项目底层配置:npm run eject

    2.下载装饰器语法babel插件:npm install @babel/plugin-proposal-decorators

    3.在packagejson文件中加入配置

    package.json

    1. "babel": {
    2. "plugins": [
    3. [
    4. "@babel/plugin-proposal-decorators",
    5. {
    6. "legacy": true
    7. }
    8. ]
    9. ]
    10. }

    开发前的准备工作 - 图1

    2.1启用装饰器语法支持(方式二)

    1.npm install react-app-rewired @babel/plugin-proposal-decorators customize-cra

    2.在项目根目录下创建config-overides.js并加入配置

    const { override, addDecoratorsLegacy } = require(“customize-cra”);

    module.exports = override(addDecoratorsLegacy());

    开发前的准备工作 - 图2

    1. package.json
    1. "scripts": {
    2. "start": "react-app-rewired start",
    3. "build": "react-app-rewired build",
    4. "test": "react-app-rewired test",
    5. }

    2.2解决vscode编辑器关于装饰器语法的警告

    修改配置:javascript.implicitProjectConfig.experimentalDecorators.true

    开发前的准备工作 - 图3