【微信小程序】小程序的 MobX 绑定辅助库

小程序的 MobX 绑定辅助库

小程序的 MobX 绑定辅助库。

此 behavior 依赖开发者工具的 npm 构建。具体详情可查阅 官方 npm 文档 。

可配合 MobX 的小程序构建版 npm 模块 mobx-miniprogram 使用。

使用方法

需要小程序基础库版本 >= 2.2.3 的环境。

也可以直接参考这个代码片段(在微信开发者工具中打开): https://developers.weixin.qq.com/s/36j8NZmC74ac

1. 安装 mobx-miniprogram 和 mobx-miniprogram-bindings :

javascript 复制代码
npm install --save mobx-miniprogram mobx-miniprogram-bindings

2.创建 MobX Store。

javascript 复制代码
// store.js
import { observable, action } from 'mobx-miniprogram'
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 构造器中使用:

javascript 复制代码
import { storeBindingsBehavior } from 'mobx-miniprogram-bindings'
import { store } from './store'
Component({
  behaviors: [storeBindingsBehavior],
  data: {
    someData: '...'
  },
  storeBindings: {
    store,
    fields: {
      numA: () => store.numA,
      numB: (store) => store.numB,
      sum: 'sum'
    },
    actions: {
      buttonTap: 'update'
    },
  },
  methods: {
    myMethod() {
      this.data.sum // 来自于 MobX store 的字段
    }
  }
})

4.在 Page 构造器中使用:

javascript 复制代码
import { createStoreBindings } from 'mobx-miniprogram-bindings'
import { store } from './store'
Page({
  data: {
    someData: '...'
  },
  onLoad() {
    this.storeBindings = createStoreBindings(this, {
      store,
      fields: ['numA', 'numB', 'sum'],
      actions: ['update'],
    })
  },
  onUnload() {
    this.storeBindings.destroyStoreBindings()
  },
  myMethod() {
    this.data.sum // 来自于 MobX store 的字段
  }
})

接口

将页面、自定义组件和 store 绑定有两种方式: behavior 绑定手工绑定

behavior 绑定
behavior 绑定 适用于 Component 构造器。做法:使用 storeBindingsBehavior 这个 behavior 和 storeBindings 定义段。

注意:你可以用 Component 构造器构造页面, 参考文档 。

javascript 复制代码
import { storeBindingsBehavior } from 'mobx-miniprogram-bindings'
Component({
  behaviors: [storeBindingsBehavior],
  storeBindings: {
    /* 绑定配置(见下文) */
  }
})

手工绑定
手工绑定 适用于全部场景。做法:使用 createStoreBindings 创建绑定,它会返回一个包含清理函数的对象用于取消绑定。

注意:在页面 onUnload (自定义组件 detached )时一定要调用清理函数,否则将导致内存泄漏!

javascript 复制代码
import { createStoreBindings } from 'mobx-miniprogram-bindings'
Page({
  onLoad() {
    this.storeBindings = createStoreBindings(this, {
      /* 绑定配置(见下文) */
    })
  },
  onUnload() {
    this.storeBindings.destroyStoreBindings()
  }
})

绑定配置

无论使用哪种绑定方式,都必须提供一个绑定配置对象。这个对象包含的字段如下:

延迟更新与立刻更新

参考文档:https://www.bookstack.cn/read/miniprogram-guide-20210305/54620b3fcfec59b9.md

相关推荐
MaCa .BaKa3 小时前
38-日语学习小程序
java·vue.js·spring boot·学习·mysql·小程序·maven
HouGISer3 小时前
副业小程序YUERGS,从开发到变现
前端·小程序
橘子海全栈攻城狮6 小时前
【源码+文档+调试讲解】党员之家服务系统小程序1
java·开发语言·spring boot·后端·小程序·旅游
好好的哦6 小时前
uni-app小程序登录后…
小程序·uni-app
h_654321021 小时前
微信小程序点击按钮跳转链接并显示
微信小程序·小程序
银迢迢1 天前
微信小程序的开发及问题解决
微信小程序·小程序
liyinchi19881 天前
原生微信小程序 textarea组件placeholder无法换行的问题解决办法
微信小程序·小程序
说私域1 天前
基于开源链动2+1模式AI智能名片S2B2C商城小程序的低集中度市场运营策略研究
人工智能·小程序·开源·零售
少恭写代码1 天前
duxapp 2025-03-29 更新 编译结束的复制逻辑等
react native·小程序·taro
suncentwl1 天前
答题pk小程序道具卡的获取与应用
小程序·答题小程序·知识竞赛