May 18, 2021 WeChat Mini Program Development Document
Page()
function is used to register a page.
Accept an object parameter that specifies the initial data, lifecycle functions, event handler, and so on for the page.
Object parameter description:
Property | Type | Describe |
---|---|---|
data | Object | The initial data for the page |
onLoad | Function | Lifecycle function - listens for page load |
onReady | Function | Life cycle function - Listen for the first rendering of the page |
onShow | Function | Lifecycle function - Listen for page display |
onHide | Function | Lifecycle function - Listen for page hiding |
onUnload | Function | Life cycle function - listen to page uninstall |
onPullDownRefresh | Function | Page-related event handler -- listens to the user's pull-down action |
onReachBottom | Function | The handler for the bottom-of-the-page event |
onShareAppMessage | Function | The user clicks on the top right corner to forward |
onPageScroll | Function | The handler of the page scroll trigger event |
Other | Any |
Developers can add any function or data to the object parameter, which can be accessed in the function
this
the page
|
Example code:
//index.js
Page({
data: {
text: "This is page data."
},
onLoad: function(options) {
// Do some initialize when page load.
},
onReady: function() {
// Do something when page ready.
},
onShow: function() {
// Do something when page show.
},
onHide: function() {
// Do something when page hide.
},
onUnload: function() {
// Do something when page close.
},
onPullDownRefresh: function() {
// Do something when pull down.
},
onReachBottom: function() {
// Do something when page reach bottom.
},
onShareAppMessage: function () {
// return custom share data when user share.
},
onPageScroll: function() {
// Do something when page scroll
},
// Event handler.
viewTap: function() {
this.setData({
text: 'Set some data for updating view.'
})
},
customData: {
hi: 'MINA'
}
})
Initializing the data is rendered as the first time the page is rendered. Data is passed from the logical layer to the rendering layer in the form of JSON, so its data must be in a format that can be converted to JSON: strings, numbers, Boolean values, objects, arrays.
The rendering layer can bind data through WXML.
Example code:
<view>{{text}}</view>
<view>{{array[0].msg}}</view>
Page({
data: {
text: 'init data',
array: [{msg: '1'}, {msg: '2'}]
}
})
onLoad
The page is loaded
onShow
The page is displayed
onReady
The page is rendered for the first time
wx.setNavigationBarTitle
are set
onReady
See Life Cycle
for details
onHide
The page is hidden
navigateTo
tab
switches.
onUnload
: Page uninstall
redirectTo
navigateBack
The call to the lifecycle and how the page is routed can be found in detail
OnLoad parameters
Type | Description |
---|---|
Object | The query parameter called by the current page is opened by the other page |
onPullDownRefresh
: pull-down refresh
enablePullDownRefresh
needs to be turned on in
config
window
options.
wx.stopPullDownRefresh
stop the pull-down refresh of the current page.
onReachBottom
: Pull-up bottom
onPageScroll
Page scroll
Field | Type | Description |
---|---|---|
scrollTop | Number | Distance at which the page has been scrolled vertically (px) |
onShareAppMessage
User forwarding
Custom forwarding fields
Field | Description | The default |
---|---|---|
title | Forward the title | The current program name |
path | The forwarding path | The current page path must be the full path that begins with / |
The sample code
Page({
onShareAppMessage: function () {
return {
title: '自定义转发标题',
path: '/page/user?id=123'
}
}
})
In addition to initializing data and lifecycle functions, there are special functions that can be defined in Page: event handler. The rendering layer can add event bindings to the component, and when the trigger event is reached, the event handler defined in page is executed.
Example code:
<view bindtap="viewTap"> click me </view>
Page({
viewTap: function() {
console.log('view tap')
}
})
route
field gets the path to the current page.
setData
function is used to send data from the logical layer to the view layer while changing the
this.data
Accept an object that changes the value of key in this.data to value in the form of key, value.
Key can be very flexible, given in the form of a data path, such as
array[2].message
a.b.c.d
and does not need to be pre-defined in this.data.
Attention:
Example code:
<!--index.wxml-->
<view>{{text}}</view>
<button bindtap="changeText"> Change normal data </button>
<view>{{num}}</view>
<button bindtap="changeNum"> Change normal num </button>
<view>{{array[0].text}}</view>
<button bindtap="changeItemInArray"> Change Array data </button>
<view>{{object.text}}</view>
<button bindtap="changeItemInObject"> Change Object data </button>
<view>{{newField.text}}</view>
<button bindtap="addNewField"> Add new data </button>
//index.js
Page({
data: {
text: 'init data',
num: 0,
array: [{text: 'init data'}],
object: {
text: 'init data'
}
},
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'
})
}
})
You don't need to figure it out right now, but it will help later.
The following illustration illustrates the life cycle of a Page instance.