当前位置: 首页 > news >正文

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

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

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

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

一、安装 miniprogram-computed

  1. 在项目的根目录下,使用如下命令,将快速在根目录下初始化生成一个 package.json 文件
    npm init -y
    
  2. 安装 miniprogram-computed
    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组件注册为 全局组件

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

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

    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 文件

    <view>{{a}} + {{b}} = {{total}}</view>
    

三、监听器 watch

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

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

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

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

    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 文件

    <view>a: {{a}}</view>
    <view>b: {{b}}</view>
    <button type="primary" bind:tap="updateData">更新数据</button>
    

相关文章:

  • 【Mybatis】动态sql
  • HarmonyOS NEXT 组件状态管理的对比
  • IoT设备测试:从协议到硬件的全栈验证体系与实践指南
  • 某公司制造业研发供应链生产数字化蓝图规划P140(140页PPT)(文末有下载方式)
  • 论文笔记(七十三)Gemini Robotics: Bringing AI into the Physical World
  • fastapi 使用 TORTOISE-ORM
  • stm32HAL库驱动gt911触摸屏
  • 麦肯锡咨询某著名企业数字化转型创新驱动与智慧企业构建(40页PPT)(文末有下载方式)
  • 计算机体系结构作业2
  • dfs(二十四)47. 全排列 II
  • 【项目合集】基于ESP32的智能盲人饮水机
  • Pygame实现记忆拼图游戏14
  • 价值流图分析VSM(75页PPT)(文末有下载方式)
  • 前端项目中应该如何选择正确的图片格式
  • 高并发编程有哪些规范?
  • LeetCode hot 100 每日一题(12)——238.除自身以外数组的乘积
  • 单调队列【C/C++】
  • 在 Linux 系统上部署 Deepseek AI 的全面指南‌
  • (* IOB = “FORCE“ *) 的使用分享
  • 鸿蒙NEXT项目实战-百得知识库04
  • 阳光保险拟设立私募证券投资基金,总规模200亿元
  • 我使馆就中国公民和企业遭不公正待遇向菲方持续提出严正交涉
  • 总奖金池百万!澎湃与七猫非虚构写作与现实题材征文大赛征稿启动
  • 高新波任西安电子科技大学校长
  • 刘国中将出席第78届世界卫生大会并顺访瑞士、访问白俄罗斯
  • Offer触手可及,2025上海社会组织联合招聘专场活动正寻找发光的你