-
WXS(WeiXin Script)是小程序的一套脚本语言, 结合 WXML, 可以构建出页面的结构, 那么如何使用呢?
-
封装common.wxs
<!-- common.wxs -->
var vailFormat = function (value) {
return value / 1000 + '千'
}
module.exports = {
vailFormat: vailFormat
}
复制代码
- example.wxml页面使用common.wxs
/* wxs是可以直接在wxml页面使用的 相当于vue.js的过滤器 wxml页面使用必须用wxs标签导入 且需要定义module名 否则无法使用 如果不使用wxs 就需要对数组进行循环 操作dom 容易出错 使用wxs很简单 方便且可以公用*/
<wxs src="common.wxs" module="common">
<view wx:for="{{list}}" wx:key="{{index}}">{{common.vailFormat(item)}}</view>
复制代码
- example.js
data: {
list: [1000, 2000, 3000, 4000]
}
复制代码