WXS(WeiXin Script)是小程序的一套脚本语言,结合 WXML,wxs类似js和js又不一样,好多js语法能在js使用不能在wxs使用可以构建出页面的结构。wxs有两种使用方式:
1. 单独的wxs文件
以.wxs为后缀的文件
var days = function (day) {
var now = getDate()
var old = getDate(day)
var min = parseInt((now - old) / 60000)
var hour = parseInt(min / 60)
var da = parseInt(min / 60 / 24)
if (min < 60) {
return min + '分钟'
} else if (hour < 24) {
return hour + '小时'
} else if (da < 30) {
return da + '天'
} else {
return parseInt(da / 30) + '月'
}
}
module.exports.days = days;
在页面中使用:
<wxs src="./cnode.wxs" module="days" />
<view >{{days.days(2001-01-09)}}</view>
页面显示return后的结果
2.在页面中直接使用
<wxs module="computed">
module.exports = {
sum: function (arr) {
var totle=0
arr.forEach(function (item) {
totle=totle+item.money
});
return totle
}
}
</wxs>
<view>{{computed.sum(option)}}</view>