You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
30 lines
716 B
30 lines
716 B
// 初始化 mobx
|
|
import { makeAutoObservable } from 'mobx'
|
|
|
|
class CounterStore {
|
|
// 定义一个原始数据 list
|
|
list = [1, 2, 3, 4, 5, 6]
|
|
|
|
// constructor() 是构造函数 作用:1.初始化this.state 2.函数方法绑定到实例。
|
|
constructor() {
|
|
// 将数据弄成响应式
|
|
makeAutoObservable(this)
|
|
}
|
|
|
|
// 定义计算属性
|
|
// get() 方法通过索引值获取动态数组中的元素。
|
|
get filterList () {
|
|
return this.list.filter(item => item > 2)
|
|
}
|
|
|
|
// 修改 list
|
|
addList = () => {
|
|
this.list.push(7, 8, 9)
|
|
}
|
|
|
|
}
|
|
|
|
// 4.实例化,然后导出给 react 使用
|
|
const counterStore = new CounterStore()
|
|
|
|
export {counterStore}
|