
如下写了两个关于时间的函数,并将它们导出,
<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()
运行微信开发者工具选择小程序项目
新建小程序项目
进入小程序的开发界面
在index.wxml页面中,添加一个按钮
名称为重新渲染
<button>重新渲染</button>
添加后的效果如图
给这个按钮添加事件
<button bindtap="onrefresh">重新渲染</button>
进入index.js文件
完成事件方法
在onrefresh中完成重新渲染的事件
, onrefresh:function(){
wx.showToast({
title: '开始重新渲染',
icon: 'success',
duration: 2000
})
this.onLoad()
}
小程序的生命周期分为 应用生命周期 、 页面生命周期
App() 必须在 app.js 中调用,必须调用且 只能调用一次 ,app.js中定义了一些应用的生命周期函数
(1)onLaunch: 初始化小程序时触发,全局只触发一次
(2)onShow: 小程序初始化完成或用户从后台切换到前台显示时触发
(3)onHide: 用户从前台切换到后台隐藏时触发
(4)onError: 小程序发生脚本错误,或者 api 调用失败时,会触发 onError 并带上错误信息
后台:点击左上角关闭,或者按了设备 Home 键离开微信,并没有直接销毁,而是进入后台
前台:再次进入微信或再次打开小程序,相当于从后台进入前台。
官方介绍 https://developers.weixin.qq.com/miniprogram/dev/reference/api/App.html
js文件中定义了一些页面生命周期函数,下面简述下这些生命周期函数的方法作用
(1)onLoad:首次进入页面加载时触发,可以在 onLoad 的参数中获取打开当前页面路径中的参数。
(2)onShow:加载完成后、后台切到前台或重新进入页面时触发
(3)onReady:页面首次渲染完成时触发
(4)onHide:从前台切到后台或进入其他页面触发
(5)onUnload:页面卸载时触发
官方介绍 https://developers.weixin.qq.com/miniprogram/dev/reference/api/Page.html
通过console.log验证它们的触发顺序
以上是对生命周期粗略的认识,如有不足请指正~
参考链接 https://www.jianshu.com/p/2e48f2468d5f
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)