1.创建工具类文件夹

备注:wxs是微信小程序中可以再wxml文件里引用的格式文件。类似vue中的过滤器。

2.在wxs文件中声明方法

// 把10000解析成1万
function formatCount(count) {
  var counter = parseInt(count);
  if (counter > 100000000) {
    return (counter / 100000000).toFixed(1) + "亿";
  } else if (counter > 10000) {
    return (counter / 10000).toFixed(1) + "万";
  } else {
    return counter + "";
  }
}

备注:微信小程序中不支持es6的部分语法,必须用function(){}的形式声明方法

3.导出方法

module.exports = {
  formatCount: formatCount
};

备注:微信小程序中不支持exports导出,必须要用module.exports={}导出

4.在需要使用的wxml文件里引用该wxs文件

<wxs src="../../utils/format.wxs" module="format"></wxs>

 <view class="count">{{format.formatCount(10000)}}</view>

效果:

 

Logo

前往低代码交流专区

更多推荐