微信小程序中使用store(类似vuex)

您所在的位置:网站首页 微信store 微信小程序中使用store(类似vuex)

微信小程序中使用store(类似vuex)

2023-04-04 18:42| 来源: 网络整理| 查看: 265

1、安装 npm install --save mobx-miniprogram mobx-miniprogram-bindings 复制代码 2、创建 MobX Store

image.png

// 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 绑定)

image.png

//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 Component 中: {{numA}} + {{numB}} = {{sum}} update 复制代码

参数说明:

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中使用(手工绑定)

image.png

//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 Page 中: {{numA}} + {{numB}} = {{sum}} update //把在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() //(在 手工绑定 中) 复制代码 7、效果

wxStore.gif



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3