zoukankan      html  css  js  c++  java
  • 「小程序JAVA实战」 小程序私有页面的生命周期以及导航(10)

    转自:https://idig8.com/2018/08/09/xiaochengxu-chuji-10/

    之前讲了小程序全局的生命周期,今天咱们说说单个页面的生命周期!源码:https://github.com/limingios/wxProgram.git 中的No.5

    Page页面的生命周期

    • 官方介绍

    https://developers.weixin.qq.com/miniprogram/dev/framework/app-service/page.html

    image.png

    • 运行小程序查看生命周期
    //index.js
    //获取应用实例
    const app = getApp()
    
    Page({
      data: {
        motto: 'Hello World',
        userInfo: {},
        hasUserInfo: false,
        canIUse: wx.canIUse('button.open-type.getUserInfo')
      },
    
      onLoad: function () {
        console.log("index->onLoad")
          this.setData({
            motto: app.globalData
          })
      },
      onReady: function () {
        console.log("index->onReady")
      },
      onShow: function () {
        console.log("index->onShow")
      },
      onHide: function () {
        console.log("index->onHide")
      },
      onUnload: function () {
        console.log("index->onUnload")
      },
    })
    
    

    加载onLoad,加载onShow,全部显示的时候调用onReady

    • 修改代码演示onHide 和 onUnload
      >增加一个绑定事件跳转的方式来演示onHide和onUnLoad
    1. navigateTo
    //index.js
    //获取应用实例
    const app = getApp()
    
    Page({
      data: {
        motto: 'Hello World',
        userInfo: {},
        hasUserInfo: false,
        canIUse: wx.canIUse('button.open-type.getUserInfo')
      },
    
      onLoad: function () {
        console.log("index->onLoad")
          this.setData({
            motto: app.globalData
          })
      },
      onReady: function () {
        console.log("index->onReady")
      },
      onShow: function () {
        console.log("index->onShow")
      },
      onHide: function () {
        console.log("index->onHide")
      },
      onUnload: function () {
        console.log("index->onUnload")
      },
      clickMe: function(){
        wx.navigateTo({
          url: '../test/test',
        })
      }
    })
    
    

    左上角有返回键

    navigateTo 可以hide

    1. redirectTo
    //index.js
    //获取应用实例
    const app = getApp()
    
    Page({
      data: {
        motto: 'Hello World',
        userInfo: {},
        hasUserInfo: false,
        canIUse: wx.canIUse('button.open-type.getUserInfo')
      },
    
      onLoad: function () {
        console.log("index->onLoad")
          this.setData({
            motto: app.globalData
          })
      },
      onReady: function () {
        console.log("index->onReady")
      },
      onShow: function () {
        console.log("index->onShow")
      },
      onHide: function () {
        console.log("index->onHide")
      },
      onUnload: function () {
        console.log("index->onUnload")
      },
      clickMe: function(){
        wx.redirectTo({
          url: '../test/test',
        })
      }
    })
    
    

    redirectTo 有onUnLoad 没有hide

    PS:这块主要是对配置的生命周期的熟悉,了解下redirectTo 和 navigateTo 之前的区别。

  • 相关阅读:
    JSONObject处理java.util.Date
    JSON lib 里JsonConfig详解
    Android编程获取手机的IMEI
    Toast用法
    JMM内存管理
    Users is not mapped(Hibernate实体类采用注解)
    指针小结(不定期更新)
    这个博客几乎不用了,转到csdn
    2013暑期在家(1)
    用户空间与内核空间,进程上下文与中断上下文[总结]
  • 原文地址:https://www.cnblogs.com/sharpest/p/10270354.html
Copyright © 2011-2022 走看看