微信小程序 声明并使用过滤器

目前微信小程序不支持管道符过滤器,不过没关系,大致都差不多

一、首先在utils文件夹下新建filters.wxs过滤器文件
微信小程序 声明并使用过滤器_第1张图片
二、编写你自己的过滤器

module.exports = {
  /**
   * 价格格式化
   * @param price
   * @returns {string}
   */
  priceFormat: function(price) {
    return Number(price).toFixed(2);
  }
}
注意:过滤方法必须写function,不然报错

三、在你需要的页面引入




合计¥{{filter.priceFormat(total)}}

 /**
   * 页面的初始数据
   */
  data: {
    total: 0
  },

最后效果
在这里插入图片描述

你可能感兴趣的:(微信小程序)