react mobx 装饰器语法配置

时间:2023-12-27 17:59:19
1.弹出项目配置

npm run eject
此处注意,若弹出项目配置失败,请先执行以下两行代码(我的项目执行上一句都会报错,所以都会执行) 1.git add .
2.git commit -m "init"
   2.安装装饰器所需依赖 npm install --save-dev babel-plugin-transform-decorators-legacy
   3.安装应用配置 npm install @babel/plugin-proposal-decorators 4.安装mobx和mobx-react 1.npm install mobx --save
2.npm install mobx-react 5.配置package.json(直接在最下方加上就行) "babel"{
"plugins":[
[
"@babel/plugin-proposal-decorators",
{
"legacy":true
}
],
[
"@babel/plugin-proposal-class-properties",
{
"loose":true
}
]
],
"presets":[
"react-app"
]
},
   6.环境测试   1.新建store.js模拟本地数据库
import {observable,action} from "mobx";
class Store {
id = Math.random();
@observable title="飞飞";
@observable finished = false;
@action cTitle =()=>{
this.title="麦扣扣"
}
}
export default new Store()
    2.新建App.js进行调用 import React, { Component } from 'react';
import Store from "./store";
import {observer} from "mobx-react" @observer
class App extends Component {
render() {
return (
<div>
this is Index
<p>{Store.id}</p>
<p>{Store.title}</p>
<button onClick={Store.cTitle}>测试</button>
</div>
);
}
}
export default App;

  注:转自https://www.cnblogs.com/ygxdbmx/p/9845241.html

搞了好久终于可以了,记录一下