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 之前的区别。

  • 相关阅读:
    Cat- Linux必学的60个命令
    Cmp- Linux必学的60个命令
    Diff- Linux必学的60个命令
    ls- Linux必学的60个命令
    mv- Linux必学的60个命令
    Find- Linux必学的60个命令
    libvirt
    PHP 设计模式 笔记与总结(2)开发 PSR-0 的基础框架
    Java实现 LeetCode 147 对链表进行插入排序
    Java实现 LeetCode 146 LRU缓存机制
  • 原文地址:https://www.cnblogs.com/sharpest/p/10270354.html
Copyright © 2011-2022 走看看