![微信小程序wxs的使用(当页面数据渲染前添加js操作),第1张 微信小程序wxs的使用(当页面数据渲染前添加js操作),第1张](/aiimages/%E5%BE%AE%E4%BF%A1%E5%B0%8F%E7%A8%8B%E5%BA%8Fwxs%E7%9A%84%E4%BD%BF%E7%94%A8%EF%BC%88%E5%BD%93%E9%A1%B5%E9%9D%A2%E6%95%B0%E6%8D%AE%E6%B8%B2%E6%9F%93%E5%89%8D%E6%B7%BB%E5%8A%A0js%E6%93%8D%E4%BD%9C%29.png)
小程序的wxs功能可以让wsmxl可以调用和编写js,基本上wxs和JS无关系,只是语法形式很相似。 如下写了两个关于时间的函数,并将它们导出, <wxs module="m1"> var getMax = function(flightDate) { var now = getDate().getDate() var flDate = getDate(flightDate).getDate() if( now <flDate ){ return '+1' }else{ return '' } } var formartTime = function(flightDate,format){ if(flightDate){ var realDate = getDate(flightDate) function timeFormat(num) { return num <10 ? '0' + num : num } var date = { "Y": timeFormat(realDate.getFullYear()), "M": timeFormat(realDate.getMonth() + 1), "d": timeFormat(realDate.getDate()), "h": timeFormat(realDate.getHours()), "m": timeFormat(realDate.getMinutes()), "s": timeFormat(realDate.getSeconds()), "q": Math.floor((realDate.getMonth() + 3) / 3), "S": realDate.getMilliseconds(), } if (!format) { format = "yyyy-MM-dd hh:mm:ss" } if( format == 'hh:mm' ){ return date.h+':'+date.m }else{ return date.h+':'+date.m } }else{ return false } } module.exports.getMax = getMax module.exports.formartTime = formartTime </wxs> 可在页面添加如下使用: m1.formartTime() m1.getMax()