微信小程序踩坑(三) —— 在小程序中使用过滤器

新建filter.wxs文件

在你想放置过滤器的目录下,创建xxx(你的文件命名).wxs文件,可以放在工具类或者是新建过滤器文件夹。

写过滤器

// 使用var定义一个json开头 将所有的过滤器写在unit中
var unit = {
  // 定义一个函数作为过滤器 参数text为传来的原始值
  reduceSecond: function(text) {
    if(text !== '' && text != undefined) {
      // 操作之后再将值return回去
      return text.substring(0, text.length - 3);
    } else {
      return text
    }
  }
}

// 导出过滤器 这段代码必写
module.exports = {
  // 格式为 导出的名字: json名字.函数名字 
  reduceSecond: unit.reduceSecond
}

调用过滤器

在需要使用过滤器的wxss文件中, 引用过滤器

<!-- src为filter.wxs的路径 绝对相对都可以 -->
<wxs module="filter" src="../../../../filter/filter.wxs" />

<view>{{filter.reduceSecond(item.text)}}</view>
posted @ 2020-03-21 10:11  仲夏今天也在写前端  阅读(3609)  评论(0编辑  收藏  举报