微信小程序计算属性与监听器: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>
相关推荐
Emma歌小白2 天前
如何首次运行小程序后端
微信小程序
赣州云智科技的技术铺子2 天前
【一步步开发AI运动APP】十二、自定义扩展新运动项目1
微信小程序·小程序·云开发·智能小程序
2501_915918412 天前
iOS 上架全流程指南 iOS 应用发布步骤、App Store 上架流程、uni-app 打包上传 ipa 与审核实战经验分享
android·ios·小程序·uni-app·cocoa·iphone·webview
00后程序员张3 天前
iOS App 混淆与加固对比 源码混淆与ipa文件混淆的区别、iOS代码保护与应用安全场景最佳实践
android·安全·ios·小程序·uni-app·iphone·webview
破无差3 天前
《赛事报名系统小程序》
小程序·html·uniapp
00后程序员张3 天前
详细解析苹果iOS应用上架到App Store的完整步骤与指南
android·ios·小程序·https·uni-app·iphone·webview
海绵宝宝不喜欢侬3 天前
uniapp-微信小程序分享功能-onShareAppMessage
微信小程序·小程序·uni-app
2501_915106323 天前
Xcode 上传 ipa 全流程详解 App Store 上架流程、uni-app 生成 ipa 文件上传与审核指南
android·macos·ios·小程序·uni-app·iphone·xcode
亮子AI3 天前
【小程序】微信小程序隐私协议
微信小程序·小程序
weixin_177297220693 天前
短剧小程序系统开发:打造个性化娱乐新平台
小程序·娱乐·短剧