1、安装
npm install --save mobx-miniprogram mobx-miniprogram-bindings
2、创建 MobX Store
// store.js
import { configure, observable, action } from 'mobx-miniprogram'
// 不允许在动作外部修改状态
configure({ enforceActions: 'observed' });
export const store = observable({
// 数据字段
numA: 1,
numB: 2,
// 计算属性
get sum() {
return this.numA + this.numB
},
// actions
update: action(function () {
const sum = this.sum
this.numA = this.numB
this.numB = sum
})
})
3、在Component中使用(behavior 绑定)
//store.js
import { storeBindingsBehavior } from 'mobx-miniprogram-bindings'
import { store } from '../../store/store'
Component({
options: {
styleIsolation: 'shared'
},
behaviors: [storeBindingsBehavior],
data: {
},
storeBindings: {
store,
fields: {
numA: () => store.numA,
numB: (store) => store.numB,
sum: 'sum'
},
actions: {
buttonTap: 'update'
},
}
})
//store.wxml
<view>Component 中:</view>
<view class="intro">{{numA}} + {{numB}} = {{sum}}</view>
<view class="intro">
<button bind:tap="buttonTap">update</button>
</view>
参数说明:
fields- fields 有三种形式:
- 数组形式:指定 data 中哪些字段来源于 store 。例如 ['numA', 'numB', 'sum'] 。
- 映射形式:指定 data 中哪些字段来源于 store 以及它们在 store 中对应的名字。例如 { a: 'numA', b: 'numB' } ,此时 this.data.a === store.numA this.data.b === store.numB 。
- 函数形式:指定 data 中每个字段的计算方法。例如 { a: () => store.numA, b: () => anotherStore.numB } ,此时 this.data.a === store.numA this.data.b === anotherStore.numB 。
- 上述三种形式中,映射形式和函数形式可以在一个配置中同时使用。
如果仅使用了函数形式,那么 store 字段可以为空,否则 store 字段必填。actions- actions 可以用于将 store 中的一些 actions 放入页面或自定义组件的 this 下,来方便触发一些 actions 。有两种形式:
- 数组形式:例如 ['update'] ,此时 this.update === store.update 。
- 映射形式:例如 { buttonTap: 'update' } ,此时 this.buttonTap === store.update 。
只要 actions 不为空,则 store 字段必填。
4、在Page中使用(手工绑定)
//store.js
import { createStoreBindings } from 'mobx-miniprogram-bindings'
import { store } from '../../store/store'
Page({
data: {
},
onLoad() {
this.storeBindings = createStoreBindings(this, {
store,
fields: ['numA', 'numB', 'sum'],
actions: ['update'],
})
},
onUnload() {
this.storeBindings.destroyStoreBindings()
}
})
//store.wxml
<view>Page 中:</view>
<view class="intro">{{numA}} + {{numB}} = {{sum}}</view>
<view class="intro">
<button bind:tap="update">update</button>
</view>
//把在component中的引入
<component />
//store.json
"usingComponents": {
"component": "/components/storeCom/store"
}
5、补充
store 绑定有两种方式: behavior 绑定 和 手工绑定 。
behavior 绑定
适用于 Component 构造器。做法:使用 storeBindingsBehavior 这个 behavior 和 storeBindings 定义段。
注意:你可以用 Component 构造器构造页面
import { storeBindingsBehavior } from 'mobx-miniprogram-bindings'
Component({
behaviors: [storeBindingsBehavior],
storeBindings: {
/* 绑定配置(见下文) */
}
})
手工绑定 适用于全部场景。做法:使用 createStoreBindings 创建绑定,它会返回一个包含清理函数的对象用于取消绑定。
注意:在页面 onUnload (自定义组件 detached )时一定要调用清理函数,否则将导致内存泄漏!
import { createStoreBindings } from 'mobx-miniprogram-bindings'
Page({
onLoad() {
this.storeBindings = createStoreBindings(this, {
/* 绑定配置(见下文) */
})
},
onUnload() {
this.storeBindings.destroyStoreBindings()
}
})
6、延迟更新与立刻更新
为了提升性能,在 store 中的字段被更新后,并不会立刻同步更新到 this.data 上,而是等到下个 wx.nextTick 调用时才更新。(这样可以显著减少 setData 的调用次数。)
如果需要立刻更新,可以调用:
this.updateStoreBindings() //(在 behavior 绑定 中)
this.storeBindings.updateStoreBindings() //(在 手工绑定 中)