【微信小程序】全局数据共享 - MobX

1. 什么是全局数据共享

2. 小程序中的全局数据共享方案

3.Mobx的使用

1.npm init -y(根据实际情况选择)

在小程序项目中,可以通过 npm 的方式引入 MobX 。

如果你还没有在小程序中使用过 npm ,那先在小程序目录中执行命令:

javascript 复制代码
npm init -y

2. 安装 MobX 相关的包

在项目中运行如下的命令,安装 MobX 相关的包:

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

安装指定版本:

3.构建npm(一定要记得)

MobX 相关的包安装完毕之后,记得删除 miniprogram_npm 目录后重新构建 npm

4.创建 MobX 的 Store 实例

  1. 根目录下创建 Store/store.js 文件。
  2. mobx-miniprogram 包中导入 observableaction两个方法。
  • observable: 用于创建 store 的实例对象
  • action: 用于包裹修改 store 数据的函数
javascript 复制代码
import { observable,action} from 'mobx-miniprogram'
// observable 的返回值就是 store 对象
export const store = observable({
  // 数据字段
  numA: 1,
  numB: 2,
  // 计算属性 get为修饰符
  get sum(){
    return this.numA + this.numB
  },
  //action方法,用来修改 store 中的数据
  updateNumA: action(function (step){
    this.numA += step
  }),
  updateNumB: action(function (step){
    this.numB += step
  })
})

5.将 Store 中的成员绑定到页面

在小程序中,将 store 成员绑定到页面中使用,可分三个步骤:

    1. 在页面 js 文件导入需要的成员。--createStoreBindings
    1. 在 onLoad 开始生命周期进行绑定。
    1. 在 onUnload 生命周期取消监听。
javascript 复制代码
// pages/message/message.js
import { createStoreBindings } from 'mobx-miniprogram-bindings'
import { store } from '../../store/store'
Page({
  addSumA(e){
    this.updateNumA(e.target.dataset.step)
  },

  /**
   * 页面的初始数据
   */
  data: {

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.storeBindings = createStoreBindings(this,{
      store,
      fields: ['numA','numB','sum'],
      actions: ['updateNumA']  // 注意是actions  不是action
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    this.storeBindings.destroyStoreBindings()
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})

其中 createStoreBindings 中的this指向当前调用该函数的实例,也就是 message 页面实例

第二个参数是一个对象,
store 代表着数据源,将 store 的属性或者方法绑定到页面实例中。
fields 是绑定到页面实例中的数据字段。
actions 是绑定到页面实例中的方法。
this.storeBindings 是接收 createStoreBindings 的返回值,并挂载在页面上,当页面卸载时需要进行清空操作。

6.在页面上使用 Store 中的成员

javascript 复制代码
<!--pages/message/message.wxml-->
<text>pages/message/message.wxml</text>
<view>{{numA}} + {{numB}} = {{sum}}</view>
<van-button bindtap="addSumA" data-step="{{1}}" type="primary">numA + 1</van-button>

7.将 Store 中的成员绑定到组件

Store 中的属性方法绑定到组件中,实现步骤可分为三个步骤:

  • 在组件 js 文件导入需要的成员。--storeBindingsBehavior
  • 在 Component 提供behaviors节点来存储前面导入的 Behaviors 数组。
  • 在 behaviors 节点平级的位置声明 storeBindings 对象接收 store、fields 和 actions 这三个属性。
javascript 复制代码
// components/my-number/my-number.js
import { storeBindingsBehavior } from 'mobx-miniprogram-bindings'
import { store } from '../../store/store'
Component({
  behaviors:[storeBindingsBehavior],
  storeBindings:{
    store,
    fields:{
      numA: ()=> store.numA,
      numB: (store) => store.numB,
      sum:'sum'
    },
    actions:{
      updateNumB:'updateNumB'
    }
  },
  /**
   * 组件的属性列表
   */
  properties: {

  },

  /**
   * 组件的初始数据
   */
  data: {

  },

  /**
   * 组件的方法列表
   */
  methods: {
    addNumB(e){
      this.updateNumB(e.target.dataset.step)
    }
  }
})

8.在组件上使用 Store 中的成员

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