原生小程序中对特定数据进行计算(wxml中wxs的使用)

背景:商品详情页对好评数进行统计,但是现在只有商品数据

使用wxs编写方法,module.exports导出,wxml中使用module名进行获取{ {goodsRate.getRate(goodsInfoList)}}

<wxs module="goodsRate">
    module.exports = {
      getRate: function (goodsInfoList) {
        if (!goodsInfoList || !goodsInfoList.basicInfo) {
          return "100"
        }
        var goodReputation = goodsInfoList.basicInfo.numberGoodReputation / goodsInfoList.basicInfo.numberOrders * 100
        goodReputation = goodReputation.toFixed(0)
        return goodReputation
      }
    }
  </wxs>
  <view id="goods-rate" class="goods-rate-box">
    <view class="label-title">
      <view class="left">宝贝评价 {
   
   {basicInfo.numberGoodReputation}}</view>
      <view class="right">好评度:{
   
   {goodsRate.getRate(goodsInfoList)}}%</view>
    </view>
  </view>

猜你喜欢

转载自blog.csdn.net/qq_34569497/article/details/135003031
今日推荐