小程序頁面生命週期---邏輯層

頁面生命週期函數就是當你每進入/切換到一個新的頁面的時候,就會調用的生命週期函數。
Page(Object) 函數用來註冊一個頁面。接受一個Object類型參數,其指定頁面的初始數據、生命週期回調、事件處理函數等。
小程序頁面生命週期函數的調用順序爲:onLoad>onReady>onShow;至於onHide函數就是當隱藏頁面的時候觸發
一般情況下:小程序周期函數在前,頁面周期函數觸發在後

//index.js
//獲取應用實例
const app = getApp()
Page({
  data: {
    text: 'init data',
    num: 0,
    array: [{ text: 'init data' }],
    object: {
      text: 'init data'
    }
  },
  onLoad(){
    console.log("=====onLoad 監聽頁面加載");
  },
  onReady(){
    console.log("=====onReady 監聽頁面初次渲染完成");
  },
  onShow() {
    console.log("=====onShow 監聽頁面顯示");
  },
  onHide() {
    console.log("=====onHide 監聽頁面隱藏");
  },
  onUnload() {
    console.log("=====onUnload 監聽頁面卸載");
  },
  changeText: function () {
    // this.data.text = 'changed data'  // bad, it can not work
    this.setData({
      text: 'changed data'
    })
  },
  changeNum: function () {
    this.data.num = 1
    this.setData({
      num: this.data.num
    })
  },
  changeItemInArray: function () {
    // you can use this way to modify a danamic data path
    this.setData({
      'array[0].text': 'changed data'
    })
  },
  changeItemInObject: function () {
    this.setData({
      'object.text': 'changed data'
    });
  },
  addNewField: function () {
    this.setData({
      'newField.text': 'new data'
    })
  }
})

在這裏插入圖片描述
在這裏插入圖片描述在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章