微信小程序状态管理与计算属性同时使用:miniprogram-computed 和 mobx-miniprogram

  • 两个框架扩展提供的 ComponentWithStoreComponentWithComputed 方法无法结合使用。
  • 如果需要在一个组件中既想使用 mobx-miniprogram-bindings 又想使用 miniprogram-computed解决方案是:
    1. 使用旧版 API

    2. 使用兼容写法

      • 即要么使用 ComponentWithStore 方法构建组件,要么使用 ComponentWithComputed 方法构建组件

      • 如果使用了 ComponentWithStore 方法构建组件,计算属性写法使用旧版 API

      • 如果使用了 ComponentWithComputed 方法构建组件,Mobx写法使用旧版 API

一、安装 miniprogram-computed 和 mobx-miniprogram

  1. 在项目的根目录下,使用如下命令,将快速在根目录下初始化生成一个 package.json 文件

    powershell 复制代码
    npm init -y
  2. 安装 miniprogram-computed 和 mobx-miniprogram

    powershell 复制代码
    npm install miniprogram-computed 
    powershell 复制代码
    npm install mobx-miniprogram mobx-miniprogram-bindings
  3. 然后 在 微信开发者工具 的左上角 点击 》工具》 构建 npm,构建成功后,将会在项目根目录下生成 miniprogram_npm 文件夹,可以在 miniprogram_npm 文件夹中看见构建的结果

二、在 ComponentWithStore 构建的组件中使用 计算属性

  1. 在项目的根目录下的 components 文件夹中(没有该文件夹的需要自己创建)新建 custom04 文件夹,并在该文件夹中创建 custom04组件(在文件夹上点击鼠标右键,选择 新建 component

  2. 找到项目根目录下的 app.json 文件,增加如下代码,将 custom04组件注册为 全局组件

    json 复制代码
    {
      // ...其他配置项
      
      "usingComponents": {
        "custom04": "./components/custom04/custom04"
      }
    }
  3. pages/index.wxml 中使用 custom04 组件

    html 复制代码
    <custom04 />
  4. 修改components/custom04/custom04.js 文件, Component 方法替换成 ComponentWithStore 方法

    js 复制代码
    // components/custom04/custom04.js
    import {
      ComponentWithStore
    } from 'mobx-miniprogram-bindings'
    
    // 导入计算属性 behavior
    const computedBehavior = require('miniprogram-computed').behavior
    
    ComponentWithStore({
      // storeBindings 不再复述
    
      // 注册 behavior
      behaviors: [computedBehavior],
      data: {
        a: 1,
        b: 2
      },
      computed: {
        total(data) {
          console.log('q23');
          return data.a + data.b
        }
      },
      watch: {
        'a,b': function (a, b) {
          console.log(`a更新之后的数据:` + a);
          console.log(`b更新之后的数据:` + b);
        }
      },
      methods: {
        updateData() {
          this.setData({
            a: this.data.a + 1,
            b: this.data.b + 1
          })
        }
      }
    
    })
  5. 修改components/custom04/custom04.wxml 文件

    html 复制代码
    <view>{{a}} + {{b}} = {{total}}</view>
    <button type="warn" bind:tap="updateData">修改数据</button>

三、在 ComponentWithComputed 构建的组件中使用 状态管理

  1. 在项目的根目录下创建 stores 文件夹,然后在该文件夹下新建 numStore.js 文件

  2. /stores/numStore.js 导入 observable action 方法。使用 observable 方法需要接受一个 store 对象,存储应用的状态

    js 复制代码
    import {
      observable,
      action
    } from 'mobx-miniprogram'
    
    export const numStore = observable({
      numA: 1,
      numB: 2,
      // 使用 action 更新 numA 以及 numB
      update: action(function () {
        this.numA += 1
        this.numB += 1
      }),
    
      // 计算属性,使用 get 修饰符,
      get sum() {
        return this.numA + this.numB;
      }
    })
  3. 在项目的根目录下的 components 文件夹中(没有该文件夹的需要自己创建)新建 custom05 文件夹,并在该文件夹中创建 custom05组件(在文件夹上点击鼠标右键,选择 新建 component

  4. 找到项目根目录下的 app.json 文件,增加如下代码,将 custom05 组件注册为 全局组件

    json 复制代码
    {
      // ...其他配置项
      
      "usingComponents": {
        "custom05": "./components/custom05/custom05"
      }
    }
  5. pages/index.wxml 中使用 custom05 组件

    html 复制代码
    <custom05 />
  6. 修改components/custom05/custom05.js 文件, Component 方法替换成 ComponentWithComputed 方法

    javascript 复制代码
    // components/custom05/custom05.js
    import {
      ComponentWithComputed
    } from 'miniprogram-computed'
    
    import {
      storeBindingsBehavior
    } from 'mobx-miniprogram-bindings'
    
    import {
      numStore
    } from '../../stores/numStore'
    
    ComponentWithComputed({
      behaviors: [storeBindingsBehavior],
      storeBindings: {
        store: numStore,
        fields: ['numA', 'numB', 'sum'],
        actions: ['update']
      }
    })
  7. 修改components/custom05/custom05.wxml 文件

    html 复制代码
    <!--components/custom05/custom05.wxml-->
    <view>{{numA}} + {{numB}} = {{sum}}</view>
    <button type="primary" bind:tap="update">更新store 中的数据</button>
相关推荐
2501_9159090613 小时前
手机崩溃日志导出的工程化体系,从系统级诊断到应用行为分析的多工具协同方法
android·ios·智能手机·小程序·uni-app·iphone·webview
toooooop814 小时前
微信小程序轮播图高度自适应优化
微信小程序·小程序
StarChainTech14 小时前
电动车租赁行业的核心需求:智能中控设备的选择与技术方案
物联网·微信小程序·小程序·软件需求·共享经济
计算机毕设指导616 小时前
基于微信小程序的积分制零食自选平台【源码文末联系】
java·spring boot·mysql·微信小程序·小程序·tomcat·maven
云起SAAS16 小时前
老年美文文章图文短视频资讯阅读抖音快手微信小程序看广告流量主开源
微信小程序·小程序·ai编程·看广告变现轻·老年美文文章图文短视频资讯阅读
qq_124987075317 小时前
基于微信小程序的民宿预订系统的设计与实现(源码+论文+部署+安装)
java·spring boot·后端·微信小程序·毕业设计
2501_9151063217 小时前
App HTTPS 抓包实战解析,从代理调试到真实网络流量观察的完整抓包思路
网络协议·http·ios·小程序·https·uni-app·iphone
游戏开发爱好者818 小时前
苹果App Store应用程序上架方式全面指南
android·小程序·https·uni-app·iphone·webview
2501_9160088918 小时前
深入理解 iPhone 文件管理,从沙盒结构到开发调试的多工具协同实践
android·ios·小程序·https·uni-app·iphone·webview
说私域18 小时前
人口红利消退与疫情冲击下电商行业的转型路径探索——以开源链动2+1模式S2B2C商城小程序为例
小程序·开源