====== 8.生命周期 ====== ===== -# 应用生命周期 ===== [[https://juejin.im/post/6844903873505656839|微信小程序实战教程]] App({ onLaunch (options) { //初始化完成后执行 }, onShow (options) { //进入小程序后执行 }, onHide () { //离开小程序后执行,注意不是销毁,如切换其他App时,微信处于后台中,这时调用onHide }, onError (msg) { //小程序出错时执行 } }); ===== -# 页面的生命周期 ===== [[https://juejin.im/post/6844903873505656839|微信小程序实战教程]] Page({ onLoad: function(options) { //页面初始化后执行 }, onReady: function() { //初次渲染结束执行 }, onShow: function() { //进入页面执行 }, onHide: function() { //页面隐藏/切入后台时触发,如 wx.navigateTo 或底部 tab 切换到其他页面,小程序切入后台等 }, onUnload: function() { //页面卸载时触发。如wx.redirectTo或wx.navigateBack到其他页面时。 }, onPullDownRefresh: function() { // Do something when pull down.监听下拉刷新事件,必须开启enablePullDownRefresh }, onReachBottom: function() { // Do something when page reach bottom. 监听用户上拉触底事件 }, onShareAppMessage: function () { // return custom share data when user share.点击转发按钮 }, onPageScroll: function() { // Do something when page scroll 页面滚动事件 }, onResize: function() { // Do something when page resize旋转屏幕触发 }, onTabItemTap(item) { console.log(item.index) console.log(item.pagePath) console.log(item.text) }, // Event handler. viewTap: function() { this.setData({ text: 'Set some data for updating view.' }, function() { // this is setData callback }) }, customData: { hi: 'MINA' } }) ===== -#3 组件生命周期 ===== [[https://juejin.im/post/6844903873505656839|微信小程序实战教程]] Component({ lifetimes: { created:function() { //组件实例化的时候执行,此时不能调用setData() } attached: function() { // 在组件实例进入页面节点树时执行 }, detached: function() { // 在组件实例被从页面节点树移除时执行 }, }, });