🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
#### WXS(WeiXin Script)是小程序的一套脚本语言,结合`WXML`,可以构建出页面的结构。 wxml中的数据,需要过滤的话,无法像vue中那样,直接添加一个函数去使用,在微信小程序中通常用wxs来过滤。 具体使用方法(不支持es6语法): 1.新建一个wxs文件,推荐建在utils中 ![](https://img.kancloud.cn/61/e2/61e267fbd10607384e46c13632f66a7d_490x320.png) ``` 案例:过滤字符串,添加对应单位 function fornatCount(count){ var countVal = parseInt(count); if(countVal > 100000000){ return (countVal / 100000000).toFixed(1) + "亿" }else if(countVal > 10000){ return (countVal / 10000).toFixed(1) + "万" }else{ return countVal + "" } } // 不支持es6,使用common.js的方式导出 module.exports = { fornatCount : fornatCount } ``` 2.在wxml文件中,通过wxs标签引入wxs文件,类似script标签 ``` module:引入的模块名,自定义,在标签中使用 <wxs src="../../utils/format.wxs" module="format"></wxs> ``` 3.使用方法 ``` format:引入时自定义的模块名 fornatCount:执行的方法名 <view class="count"> 播放:{{format.fornatCount(item.playCount) }}</view> ``` 效果图: ![](https://img.kancloud.cn/ac/8a/ac8a1a74a57e1b25e201a1551d781a88_574x314.png)