微信小程序wxs的使用(当页面数据渲染前添加js *** 作)

微信小程序wxs的使用(当页面数据渲染前添加js *** 作),第1张

程序的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()

方法:获取page数据,看并肢代码:var text=this.data.name  ,这样就获取到初始化的值

page({

data:{name:"猜配test"},

showData:function(){

var text=this.data.name

}

})

示例如下:

util.js

//正则判断

function Regular(str, reg) {

if (reg.test(str))

return true

return false

}

//是否为中文

function IsChinese(str) {

var reg = /^[\u0391-\uFFE5]+$/

return Regular(str, reg)

}

module.exports = {

getRequestUrl: "http://localhost:59637",//获得接口地址

IsChinese: IsChinese,

}

//test.js

var util = require('../../utils/util.js')

Page({

onLoad: function () {

console.log("判断是否为中绝兆世文:"+util.IsChinese('测试'))

console.log("输出接口url:"+util.getRequestUrl)

}

})


欢迎分享,转载请注明来源:内存溢出

原文地址: http://www.outofmemory.cn/yw/12561850.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-26
下一篇 2023-05-26

发表评论

登录后才能评论

评论列表(0条)

保存