React-使用装饰器

create-react-app默认不支持装饰器的,需要做以下配置。
打开 package.json ,可以看到eject。运行 npm run eject 可以让由create-react-app创建的项目的配置项暴露出来。

安装babel插件

  Babel >= 7.x
npm install --save-dev @babel/plugin-proposal-decorators

  [email protected]

npm install --save-dev babel-plugin-transform-decorators-legacy

修改package.json文件的babel配置项
  Babel >= 7.x

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

  [email protected]

"babel": { "plugins": [ "transform-decorators-legacy" ], "presets": [ "react-app" ]
  }

至此,就可以在项目中使用装饰器了

@MyContainer
class B extends Component{
  render(){ return ( 

B组件

) } } export default B;

你可能感兴趣的:(react.js)