小程序 通过计算属性 改变状态

<!-- 工期剩余 -->
<!-- 还剩下3天的时候变成红色 -->
<template>
  <div class="day-residue-tag">
    <span>{{stateTitle}}</span>
    <span>{{residueTime}}</span>
  </div>
</template>
<script>
export default {
  props: {
    // 为10的时候,是完工
    state: {
      name: '订单状态',
      type: Number
    },
    expect_at: {
      name: '工期剩余',
      type: String
    },
    finish_at: {
      name: '完工时间',
      type: String
    }
  },
  computed: {
    stateTitle() {
      if (this.state == 10) {
        return '完成时间'
      }
      return '工期剩余'
    },
    // 剩余时间
    residueTime() {
      if (this.state > 6) {
        if (this.finish_at) {
          return this.timeF(this.finish_at)
        }
        else return '--'
      }
      // 计算剩余时间差
      const diff = new Date(this.expect_at).getTime() - new Date().getTime();
      return this.countDownText(diff)
    }
  },
  methods: {
    // 倒计时时间显示处理
    countDownText(diff) {
      const d = Math.floor(diff / (60 * 1000 * 60 * 24));
      const ddiff = diff % (60 * 1000 * 60);
      const h = Math.floor(ddiff / (60 * 1000 * 60 * 24));
      const hdiff = diff % (60 * 1000 * 60);
      const m = Math.floor(hdiff / (60 * 1000));
      const mdiff = hdiff % (60 * 1000);
      const s = mdiff / (1000);
      const sCeil = Math.ceil(s);
      const sFloor = Math.floor(s);
      return `${d}天`
    },
  }
}
</script>
<style lang="less" scoped>
  .day-residue-tag {
  }
</style>



### 微信小程序计算属性使用 在微信小程序中,虽然不像 Vue.js 那样有显式的 `computed` 属性概念,但可以借助 WXS 或者通过页面逻辑层的数据绑定和事件处理机制来实现类似的计算属性功能。 #### 使用 WXS 实现计算属性 WXS (WeiXin Script) 提供了一种类似于 JavaScript 的脚本语言,专门用于数据处理。由于其独立于基础库版本且具有较高的执行效率,特别是在 iOS 上,因此非常适合用来做复杂的计算操作[^2]。 下面是一个简单的购物车示例,展示了如何利用 WXS 来创建一个计算属性: 假设有一个商品列表,每个商品都有价格 (`price`) 和数量 (`count`) 字段。现在想要实时显示总价 (`totalPrice`) ,即所有选中的商品的价格总和。 ##### 页面结构(wxml) ```html <view> <!-- 商品项 --> <block wx:for="{{goods}}" wx:key="id"> <view class="item"> {{item.name}} - 单价:{{item.price}} 数量:<input type="number" value="{{item.count}}" bindchange="onChangeCount"/> </view> </block> <!-- 总价展示 --> <view>总价:{{getTotalPrice()}}</view> </view> ``` ##### 数据模型(js) ```javascript Page({ data: { goods: [ { id: '1', name: "苹果", price: 5, count: 2 }, { id: '2', name: "香蕉", price: 3, count: 4 } ] }, onChangeCount(e){ const index = e.currentTarget.dataset.index; let newGoods = this.data.goods.map((good,i)=>{ if(i===index){ good.count=parseInt(e.detail.value); } return good; }); this.setData({ goods:newGoods }); } }) ``` ##### 定义 WXS 函数(wxs) 为了简化代码并提高性能,这里我们将计算总价的功能封装到一个单独的 `.wxs` 文件里: ```javascript // totalPrice.wxs module.exports.getTotalPrice = function(goodsList){ var total = 0; for(var i=0; i<goodsList.length;i++){ total += goodsList[i].price * goodsList[i].count; } return total; } ``` 最后,在 wxml 中引入此模块并通过它获取最终的结果: ```html <wxs src="./totalPrice.wxs" module="calc"></wxs> ... <!-- 显示总价的地方 --> <view>总价:{{ calc.getTotalPrice(goods) }}</view> ``` 这样就实现了基于当前状态动态更新视图的效果,而无需每次修改商品数量时都手动触发重新渲染整个组件树的操作.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值