# Registration page
Each page in Weixin Mini Program needs to be registered in thejsfile corresponding to the page, specifying the page's initial data, lifecycle callbacks, event handling functions, and so on.
# Register a page with the Page constructor
Simple pages can be constructed usingPage ().
Code example:
//index.js
Page({
data: {
text: "This is page data."
},
onLoad: function(options) {
// 页面创建时执行
},
onShow: function() {
// 页面出现在前台时执行
},
onReady: function() {
// 页面首次渲染完毕时执行
},
onHide: function() {
// 页面从前台变为后台时执行
},
onUnload: function() {
// 页面销毁时执行
},
onPullDownRefresh: function() {
// 触发下拉刷新时执行
},
onReachBottom: function() {
// 页面触底时执行
},
onShareAppMessage: function () {
// 页面被用户分享时执行
},
onPageScroll: function() {
// 页面滚动时执行
},
onResize: function() {
// 页面尺寸变化时执行
},
onTabItemTap(item) {
// tab 点击时执行
console.log(item.index)
console.log(item.pagePath)
console.log(item.text)
},
// 事件响应函数
viewTap: function() {
this.setData({
text: 'Set some data for updating view.'
}, function() {
// this is setData callback
})
},
// 自由数据
customData: {
hi: 'MINA'
}
})
Please refer to Page Reference document for detailed parameter meaning and usage.
# Using behaviors in a page
Start from base library version 2.9.2. Please remaining backward compatible.
Pages can reference behaviors. Behaviors can be used to make multiple pages have the same data fields and methods.
// my-behavior.js
module.exports = Behavior({
data: {
sharedText: 'This is a piece of data shared between pages.'
},
methods: {
sharedMethod: function() {
this.data.sharedText === 'This is a piece of data shared between pages.'
}
}
})
// page-a.js
var myBehavior = require('./my-behavior.js')
Page({
behaviors: [myBehavior],
onLoad: function() {
this.data.sharedText === 'This is a piece of data shared between pages.'
}
})
For specific usage see behaviors .
# Constructing a page with the Component constructor
Start from base library version 1.6.3. Please remaining backward compatible.
The pageconstructor is suitable for simple pages.But for complex pages, thePageconstructor may not work well.
At this point, you can use theComponentconstructor to construct the page.ComponentThe main difference between constructors is that methods need to be placed insidemethods: {}.
Code example:
Component({
data: {
text: "This is page data."
},
methods: {
onLoad: function(options) {
// 页面创建时执行
},
onPullDownRefresh: function() {
// 下拉刷新时执行
},
// 事件响应函数
viewTap: function() {
// ...
}
}
})
This way of creating is very similar to custom components , which can use advanced features such asbehaviorsjust like custom components.
For details, please read ComponentConstructor section.