关于react.js: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

  Babel@6.x

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" ]
  }

  Babel@6.x

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

至此,就能够在我的项目中应用装璜器了

@MyContainer
class B extends Component{
  render(){ return ( <p>B组件</p>
 )
  }
}
export default B;

评论

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

这个站点使用 Akismet 来减少垃圾评论。了解你的评论数据如何被处理