微信小程序实现wxml视图向js传递参数的方法(点击view带参数进入js)

JavaScript011

微信小程序实现wxml视图向js传递参数的方法(点击view带参数进入js),第1张

一:url带参数传递

与前端语言一样,小程序页面间的传递可以通过在路由url后接参数,路由的同时会将参数一并传递到新的页面。

index.wxml:

效果图:

二:将值存入全局变量

我们同样可以将需要的值存入全局变量中,在需要的地方直接引用就好了。

需要用到全局变量时记得要先获取应用实例: const app = getApp()

**三, 用event传值

JS文件

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