如何在react中使用decorator
2024-10-21 14:27:39
2020-03-27
如何在react中使用decorator
decorator目前都需要修改babel才能使用
说一下具体的操作方法 踩了一天的坑。。。
步骤1: yarn create react-app myapp
习惯用yarn npm也行 都一样
步骤2: yarn add @babel/plugin-proposal-decorators -D
装依赖!!!
步骤3: yarn eject 或者 修改node_modules
先说yarn eject, 将配置文件暴露到项目中
执行完成之后,修改package.json中的babel 修改如下
"babel": {
"presets": [
"react-app"
],
"plugins": [
[
"@babel/plugin-proposal-decorators",
{
"legacy": true
}
]
]
},
但是,react的默认配置看的人头晕,如果不想eject
修改 node_modules -> react-scripts -> config -> webpack.config.js
找到 test: /\.(js|mjs|jsx|ts|tsx)$/ 在下面的plugins中加入新的配置 如下红色部分
{
test: /\.(js|mjs|jsx|ts|tsx)$/,
include: paths.appSrc,
loader: require.resolve('babel-loader'),
options: {
customize: require.resolve(
'babel-preset-react-app/webpack-overrides'
),
babelrc: false,
configFile: false,
presets: [require.resolve('babel-preset-react-app')],
cacheIdentifier: getCacheIdentifier(
isEnvProduction
? 'production'
: isEnvDevelopment && 'development',
[
'babel-plugin-named-asset-import',
'babel-preset-react-app',
'react-dev-utils',
'react-scripts',
]
),
// @remove-on-eject-end
plugins: [
[
require.resolve('babel-plugin-named-asset-import'),
{
loaderMap: {
svg: {
ReactComponent:
'@svgr/webpack?-svgo,+titleProp,+ref![path]',
},
},
},
],
[
"@babel/plugin-proposal-decorators",
{
"legacy": true
}
]
],
// This is a feature of `babel-loader` for webpack (not Babel itself).
// It enables caching results in ./node_modules/.cache/babel-loader/
// directory for faster rebuilds.
cacheDirectory: true,
// See #6846 for context on why cacheCompression is disabled
cacheCompression: false,
compact: isEnvProduction,
},
},
大功告成,可以愉快的用decorator了
最新文章
- lintcode 落单的数(位操作)
- javascript中,对于this指向的浅见
- 将对象转为数组方法:延伸array_map函数在PHP类中调用内部方法
- oracle的基本查询~上
- Jquery源码中的Javascript基础知识(二)
- This Handler class should be static or leaks might occur Android
- Reshape the Matrix
- Loj #3055. 「HNOI2019」JOJO
- MySQL 查询语句中自己定义的中文内容在Java Web 中显示为问号
- Hdoj 2602.Bone Collector 题解
- Oracle 学习笔记(六)
- 17.泛型.md
- google chrome 删除重复的书签 about sync
- Android开发进阶从小工到专家之性能优化
- 坐标转换convertRect
- NSOperation的使用细节 [1]
- jq实现随机显示部分图片在页面上(兼容IE5)
- 【好】strong-password-checker,我自己做出来的:)
- LeetCode - 20. Valid Parentheses(0ms)
- innodb_stats_on_metadata and slow queries on INFORMATION_SCHEMA
热门文章
- 轻松解决python异常处理,你值得拥有
- linux DRM/KMS 测试工具 modetest、kmscude、igt-gpu-tools (一)
- poj2455 k条路最小化最长边
- mysql单记录也能造成的死锁
- 4.4MSSQLServer常用版本介绍
- Centos 安装 docker 和 docker-compose
- Shone.Math开源系列2 — 实数类型(含分数和无理数)的实现
- 基于 kubeadm 搭建高可用的kubernetes 1.18.2 (k8s)集群一 环境准备
- 括号树 noip(csp??) 2019 洛谷 P5658
- Java实现 LeetCode 796 旋转字符串 (水题)