Error: The 'decorators' plugin requires a 'decoratorsBeforeExport' option

本文解决了一个在React项目中使用Babel7时遇到的装饰器插件错误。详细介绍了如何安装并配置正确的插件来避免错误,包括使用‘decorators-legacy’插件替代‘decorators’插件的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Error: The ‘decorators’ plugin requires a ‘decoratorsBeforeExport’ option, whose value must be a boolean. If you are migrating from Babylon/Babel 6 or want to use the old decorators proposal, you should use the ‘decorators-legacy’ plugin instead of ‘decorators’

背景:react 项目 + babel 7 + 使用到装饰器

错误

在这里插入图片描述
使用以上装饰器时,需安装相应的插件和配置某些文件,否则可能会出现以下错误
在这里插入图片描述

解决办法

步骤如下:

1、安装以下插件

	npm install -D @babel/plugin-proposal-decorators

2、配置.babelrc 文件或package.json文件的 babel 字段值(两者二选一,不要都配置,否者会报找到两个配置文件的错)
(1)配置 .babelrc 文件

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

(2)配置 package.json 文件的 babel 字段值

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

参考文章
‘decorators-legacy’ issue

引用\[1\]:报错:Parsing error: This experimental syntax requires enabling one of the following parser plugin(s): "decorators-legacy", "decorators". 引用\[2\]:在学习mobx时,遇到了“This experimental syntax requires enabling one of the following parser plugin(s): "decorators", "decorators-legacy". (5:0)“的错误,每次在@observer那里报错。 引用\[3\]:但是发现还是不稳定,可以试试这个React的decorators装饰器报错@_程序燕的博客-CSDN博客_react 装饰器报错Parsing error: This experimental syntax requires enabling one of the following parser plugin(s): "decorators-legacy", "decorators".对修饰器的实验支持功能在将来的版本中可能更改。在 "tsconfig" 或 "jsconfig" 中https://blog.csdn.net/lfy_wybss/article/details/122079178?spm=1001.2101.3001.6661.1&utm_medium=distribute.pc_relevant_t0.none-task-blog-2%7Edefault%7EBlogCommendFromBaidu%7Edefault-1-122079178-blog-111914160.pc_relevant_scanpaymentv1&depth_1-utm_source=distribute.pc_relevant_t0.none-task-blog-2%7Edefault%7EBlogCommendFromBaidu%7Edefault-1-122079178-blog-111914160.pc_relevant_scanpaymentv1&utm_relevant_index=1。 问题:Parsing error: This experimental syntax requires enabling one of the following parser plugin(s): "decorators", "decorators-legacy"是什么意思?如何解决这个错误? 回答: 当你遇到这个错误时,它意味着你的代码中使用了修饰器语法,但是你的解析器没有启用修饰器插件。修饰器是一种实验性的语法,需要通过启用相应的插件来支持。在你的代码中,你可以尝试启用"decorators"或"decorators-legacy"插件来解决这个错误。你可以在你的"tsconfig"或"jsconfig"文件中进行配置。具体的配置方法可以参考这篇博客文章:https://blog.csdn.net/lfy_wybss/article/details/122079178。 #### 引用[.reference_title] - *1* *3* [mobx装饰器报错](https://blog.csdn.net/qq_45679015/article/details/124803555)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [react装饰器报错:This experimental syntax requires enabling one of the following parser plugin(s):...](https://blog.csdn.net/liuye066/article/details/127889894)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值