博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
关于Create React App不支持装饰器的终极无伤解决方案
阅读量:6915 次
发布时间:2019-06-27

本文共 1248 字,大约阅读时间需要 4 分钟。

之前发过一篇解决这个问题的文章,但是对create react app 都是有侵入的。

这里提出一个我觉得是最OK的解决方案,一波带走。

关于Create React App不支持装饰器的解决方案

找到node_modules/react-scripts/config/webpack.config.dev.js文件。

观察其JSX|JS的loader配置。

// Process JS with Babel.{  test: /\.(js|jsx)$/,  include: paths.appSrc,  loader: require.resolve('babel-loader'),  options: {    // @remove-on-eject-begin    babelrc: false,    presets: [require.resolve('babel-preset-react-app')], <===== 注意这里!!    // @remove-on-eject-end    // 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,  },},复制代码

OK! 我们找到node_modules/babel-preset-react-app/index.js,然后加入装饰器支持。

/** * Copyright (c) 2015-present, Facebook, Inc. * * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. */'use strict';const plugins = [  // 增加装饰器的支持  require.resolve('babel-plugin-transform-decorators-legacy'), <=== 注意这里!  // class { handleClick = () => { } }  require.resolve('babel-plugin-transform-class-properties')  ....复制代码

记着在对应的package.json下边加入babel-plugin-transform-decorators-legacy。

然后 cnpm install 再试试使用装饰器,你会发现OK啦。这是目前侵入最小的解决方案。

转载地址:http://jiacl.baihongyu.com/

你可能感兴趣的文章
我的友情链接
查看>>
SpringMVC同名参数绑定问题
查看>>
软件级负载均衡器(LVS/HAProxy/Nginx)的特点简介和对比
查看>>
关于Session和Cookie简单实例
查看>>
每天多一点
查看>>
SpringMVC统一异常处理
查看>>
到底什么是自动化运维
查看>>
Linux下给mysql创建用户分配权限
查看>>
python学习笔记
查看>>
rm 命令
查看>>
FTP上传下载shell脚本
查看>>
CentOS 6.3下vsftpd虚拟用户架设
查看>>
ActiveMQ之虚拟主题
查看>>
白话理解JVM工作原理
查看>>
automak语法
查看>>
vue+eltree
查看>>
《阿里专有云等保合规白皮书》发布,阿里云实现首个一体化云原生安全架构...
查看>>
宁宇:我来谈谈去IOE
查看>>
Redis持久化及数据恢复
查看>>
常用的位运算:int与byte[]互相转换
查看>>