微信小程序计算属性与监听器:miniprogram-computed

  • 小程序框架没有提供计算属性相关的 api ,但是官方为开发者提供了拓展工具库 miniprogram-computed

  • 该工具库提供了两个功能:

    1. 计算属性 computed
    2. 监听器 watch

一、安装 miniprogram-computed

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

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

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

二、计算属性 computed

  • 如果需要在组件中使用计算属性功能,需要 miniprogram-computed 库中导入 ComponentWithComputed 方法

  • 在使用时:需要将 Component 方法替换成 ComponentWithComputed 方法 ,原本组件配置项也需要写到该方法中。在替换以后,就可以新增 computed 以及 watch 配置项。

    注意事项

    ​ 1.computed 函数中不能访问 this ,但是提供了形参,代表 data 对象

    ​ 2.计算属性函数的返回值会被设置到 this.data.sum 字段中

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

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

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

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

    javascript 复制代码
    import {
      ComponentWithComputed
    } from 'miniprogram-computed'
    
    ComponentWithComputed({
      data: {
        a: 1,
        b: 2
      },
      // 计算属性
      computed: {
        total(data) {
          // 不能使用 this 获取数据
          console.log(this); // undefined
          return data.a + data.b
        }
      },
      methods: {}
    })
  5. 修改 components/custom02/custom02.wxml 文件

    html 复制代码
    <view>{{a}} + {{b}} = {{total}}</view>

三、监听器 watch

  • 在使用时:需要将 Component 方法替换成 ComponentWithComputed 方法 ,原本组件配置项也需要写到该方法中,在替换以后,就可以新增 computed 以及 watch 配置项。
  1. 在项目的根目录下的 components 文件夹中(没有该文件夹的需要自己创建)新建 custom03 文件夹,并在该文件夹中创建 custom03组件(在文件夹上点击鼠标右键,选择 新建 component

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

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

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

    javascript 复制代码
    import {
      ComponentWithComputed
    } from 'miniprogram-computed'
    
    ComponentWithComputed({
      data: {
        a: 1,
        b: 2
      },
      watch: {
        // key: 需要监听的数据
        // value: 回调函数,参数时改变之后的数据
        // a: function (newVal) {
        //   console.log(`a更新之后的数据:` + newVal);
        // },
        // b: function (newVal) {
        //   console.log(`b更新之后的数据:` + newVal);
        // }
    
        // 监听多个数据
        "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/custom03/custom03.wxml 文件

    html 复制代码
    <view>a: {{a}}</view>
    <view>b: {{b}}</view>
    <button type="primary" bind:tap="updateData">更新数据</button>
相关推荐
2501_915918413 小时前
iOS描述文件功能解析
android·macos·ios·小程序·uni-app·cocoa·iphone
说私域7 小时前
开源AI智能客服、AI智能名片与S2B2C商城小程序在营销运营中的应用与重要性研究
人工智能·小程序·开源
说私域7 小时前
开源AI智能名片链动2+1模式S2B2C商城小程序商业化路径优化研究
人工智能·小程序·开源
Y_3_712 小时前
微信小程序动态二维码外部实时展示系统
微信小程序·小程序·notepad++
weixin_177297220691 天前
旧物二手回收小程序:引领绿色消费,开启时尚生活新方式
小程序·盲盒
2501_916007471 天前
Fastlane 结合 开心上架(Appuploader)命令行实现跨平台上传发布 iOS App 的完整方案
android·ios·小程序·https·uni-app·iphone·webview
韩立学长1 天前
【开题答辩实录分享】以《植物病虫害在线答疑小程序的设计与实现》为例进行答辩实录分享
spring boot·小程序·vue
好想早点睡.1 天前
vue2+UniApp微信小程序集成高德地图
微信小程序·小程序·uni-app
李慕婉学姐1 天前
【开题答辩过程】以《基于微信小程序的线上讲座管理系统》为例,不会开题答辩的可以进来看看
javascript·mysql·微信小程序
2501_915918411 天前
iOS 上架应用市场全流程指南,App Store 审核机制、证书管理与跨平台免 Mac 上传发布方案(含开心上架实战)
android·macos·ios·小程序·uni-app·cocoa·iphone