react 应用中使用装饰器

1. 在不eject的情况下,网友给出了一个修改node_modules的解决方案:

找到node_modules/babel-preset-react-app/index.js,然后加入装饰器支持;接着在对应的package.json下边加入babel-plugin-transform-decorators-legacy。

 总结:node_modules/babel-preset-react-app/index.js还有对应的package.json 加上decorators插件,然后运行就可以了。

2. 在eject的情况下:

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

b.package.json修改:

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

参考资料:

https://juejin.im/post/59faf3975188254eaf27ea71

https://www.robinwieruch.de/create-react-app-mobx-decorators/ 需要翻墙,写的很好很全面.


猜你喜欢

转载自blog.csdn.net/openglnewbee/article/details/79529802