定义变量
@observable price = 0;
@observable amount = 1;
import React from 'react';
import ReactDOM from 'react-dom';
import {observable,action,makeAutoObservable} from 'mobx'
import {observer}from 'mobx-react'
//1.初始化mobx仓库
class Store {
@observable count = 0
//一定要加这一行
constructor(){
makeAutoObservable(this)
}
@action
increment=()=>{
this.count++
}
}
@observer
class App extends React.Component{
render(){
const {store}=this.props
return(
<div>
<h1>我是一个仓库</h1>
<h2>{store.count}</h2>
<button onClick={store.increment}>增加</button>
</div>
)
}
}
ReactDOM.render(
<App store={new Store()}/>,
document.getElementById('root')
);