小程序如何使用WXS

432 阅读1分钟
  1. WXS(WeiXin Script)是小程序的一套脚本语言, 结合 WXML, 可以构建出页面的结构, 那么如何使用呢?

  2. 封装common.wxs

<!-- common.wxs -->
var vailFormat = function (value) {
    return value / 1000 + '千'
}

module.exports = {
  vailFormat: vailFormat  
}
  1. 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>
  1. example.js
data: { 
  list: [1000, 2000, 3000, 4000]
}