在新建的react脚手架中引入mobx的装饰器,会报以下错误
Support for the experimental syntax 'decorators-legacy' isn't currently enabled
这是因为我们没有装支持装饰器的组件,需要完成以下几步:
1. 运行npm run eject 暴露配置文件
注意:在新建的react脚手架中,运行npm run eject
命令会报错
Remove untracked files, stash or commit any changes, and try again.
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! my-app@0.1.0 eject: `react-scripts eject`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the my-app@0.1.0 eject script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
原因:主要是脚手架添加 .gitgnore文件,但是却没有本地仓库
解决办法:
npm i -g create-react-app
create-react-app my-app
cd my-app
git init
git add .
git commit -m 'xx'
npm run eject
2. 正确安装babel
npm i @babel/core @babel/plugin-proposal-class-properties @babel/plugin-proposal-decorators @babel/preset-env
3. 将package.json中的babel依赖修改为
"babel": {
"presets": [
"react-app"
],
"plugins": [
[
"@babel/plugin-proposal-decorators",
{
"legacy": true
}
]
]
},