使用微信小程序 WXS模块构建过滤器

版权声明: https://blog.csdn.net/qq_23521659/article/details/82185004

参考:

https://www.w3cschool.cn/weixinapp/weixinapp-nvg32czm.html

因为后台每次传时间都是long型的,而且在数组对象里,需要遍历,处理这些时间戳的方式只能在遍历出来之后;

wxs:

var DateFr = {

  getDate: function (time, splitStr) {
    console.log(time)

    if (!time) return '';

    var date = getDate(time);
    var M = date.getMonth() + 1;
    var y = date.getFullYear();
    var d = date.getDate();

    if (M < 10) M = "0" + M;
    if (d < 10) d = "0" + d;

    if (splitStr)
      return y + splitStr + M + splitStr + d;
    else
      return {
        y: y,
        M: M,
        d: d
      };
  }
}
module.exports = {
  getDate: DateFr.getDate,
}

使用的时候,在wxml引入:

<wxs module="dateFr" src="../../filter/dateFr.wxs"></wxs>

然后就可以达到过滤器一样的效果了:

<view><button>已结束</button>{{dateFr.getDate(item.startTime*1000,'-')}}</view>

(注:在模板中使用无效。)

猜你喜欢

转载自blog.csdn.net/qq_23521659/article/details/82185004
今日推荐