对实验性语法‘装饰器-遗留版本’的支持当前未启用
我试着用{ legacy: true }
在.babelrc
中添加decorators-legacy
babel插件和@babel/plugin-proposal-decorators
,但是没有效果。
有没有人能让MobX装饰器和CRA2一起工作?
发布于 2019-03-11 02:55:06
首先,安装依赖项:
yarn add react-app-rewired customize-cra @babel/plugin-proposal-decorators
其次,在根目录下创建config-overrides.js
,内容如下:
const {
addDecoratorsLegacy,
override,
disableEsLint
} = require("customize-cra");
module.exports = {
webpack: override(
addDecoratorsLegacy(),
disableEsLint()
)
};
你现在应该可以使用mobx +装饰器了。
如果你还没有安装mobx,请运行:yarn add mobx mobx-react
__。现在你可以使用装饰器了。
发布于 2018-10-11 08:44:37
我也有同样的问题,最终我使用了mobx4,装饰器可以在没有装饰器语法的情况下使用。
class Store {
//...
@action empty() {
this.data = []
}
@action add(e) {
this.data.push(e)
}
}
可以重写为
class Store {
//...
empty() {
this.data = []
}
add(e) {
this.data.push(e)
}
}
decorate(Store, {
add: action,
empty: action
})
感谢Michel Weststrate提供的这些东西
https://medium.com/@mweststrate/mobx-4-better-simpler-faster-smaller-c1fbc08008da
发布于 2019-06-25 13:46:15
选项1:在CRA v2中使用装饰器
如果使用Mobx documentation,可以通过使用Typescript让Mobx装饰器与CRAv2一起使用
只有在create-react-app@^2.1.1中使用TypeScript时,才支持
装饰器。
但是,在某些情况下,在将Mobx与其他react框架一起使用时,您可能仍然会遇到问题。在这种情况下:
装饰器选项2:不使用装饰器
详细的分步指南记录在here中。
如果您之前将观察者react组件定义为:
@observer
export default class MyComponent extends React.Component
将其更改为:
const MyComponent = observer(class MyComponent extends React.Component{
/* ... */
});
export default MyComponent;
如果您以前有:
@observable myElement = null;
您需要将其更改为:
myElement;
然后:
decorate(MyComponent, {
myElement: observable,
})
希望这能有所帮助!
https://stackoverflow.com/questions/52641907
复制相似问题