页面生命周期函数就是当你每进入/切换到一个新的页面的时候,就会调用的生命周期函数。
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'
})
}
})