我一朋友开发过程中问我有更好的方式解决太多的条件渲染,如图
然我举荐他使用wxs
wxs使用方法
新建一个***.wxs
var filters = {
/**保留小数点*/
toFix: function(value, digit) { /**保留小数点*/
return value.toFixed(digit)
},
/**数组切割*/
toArr: function(arr, str) {
return arr.join(str)
},
/**转换数字*/
toNumber: function(val) {
console.log('=======', val);
return Number(val);
},
/**时间差*/
timeRub: function(this_time, past_time) {
var past_times = getDate(past_time).getTime(),
this_times = getDate(this_time).getTime(),
rest = this_times - past_times,
unit;
if (rest >= 1000 && rest < 60000) {
unit = Math.floor(rest / 1000) + '秒前';
} else if (rest >= 60000 && rest < 3600000) {
unit = Math.floor(rest / 60000) + '分钟前';
} else if (rest >= 3600000 && rest < 86400000) {
unit = Math.floor(rest / 3600000) + '小时前';
} else if (rest >= 86400000) {
unit = Math.floor(rest / 86400000) + '天前';
if (Math.floor(rest / 86400000) == 30) {
unit = '1个月前'
}
if (Math.floor(rest / 86400000) > 30) {
var date = getDate(past_time),
Y = date.getFullYear() + '-',
M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-',
D = date.getDate() + ' ';
unit = Y + M + D;
}
}
return unit;
},
}
module.exports = {
toFixs: filters.toFix,
toArrs: filters.toArr,
toNumber: filters.toNumber,
timeRub: filters.timeRub
}
然后在用到的wxml上引入
再让后在其使用效果
还可以做一些比较麻烦的渲染判断,如下的业务是把用户选多个答案进行渲染判断
wxs 模块
wxml模块