Home >WeChat Applet >Mini Program Development >page() function for small program development
Page
Page() function is used to register a page. Accepts an object parameter, which specifies the initial data, life cycle function, event processing function, etc. of the page.
Object parameter description:
Sample 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 date when user share. }, // Event handler. viewTap: function() { this.setData({ text: 'Set some data for updating view.' }) }, customData: { hi: 'MINA' } })
Initialization data
Initialization data will be used as the first rendering of the page. The data will be transmitted from the logic layer to the rendering layer in the form of JSON, so the data must be in a format that can be converted into JSON: String, numbers, Boolean values, Object , Array.
The rendering layer can bind data through WXML.
Sample code:
<view>{{text}}</view><view>{{array[0].msg}}</view> Page({ data: { text: 'init data', array: [{msg: '1'}, {msg: '2'}] } })
Life cycle function
onLoad: Page loading
A page will only be called once.
Receiving page parameters can obtain wx.navigateTo, wx.redirectTo and query in
onShow: Page display
will be called every time the page is opened.
onReady: The initial rendering of the page is completed
A page will only be called once, which means that the page is ready and can interact with the view layer.
Please set interface settings such as wx.setNavigationBarTitle after onReady. For details, see Life Cycle
onHide: Page Hide
Called when navigateTo or the bottom tab is switched.
onUnload: Page unloading
Called when redirectTo or navigateBack.
Life cycle calls and page routing methods are detailed in the
onLoad parameter
Page related event processing function
onPullDownRefresh: Pull-down refresh
Listen to user pull-down refresh events.
You need to enable enablePullDownRefresh in the window option of config.
When the data refresh is processed, wx.stopPullDownRefresh can stop the pull-down refresh of the current page.
onShareAppMessage: User sharing
Only if this event handler is defined, the "Share" button will be displayed in the upper right corner menu
When the user clicks the share button, it will be called
This event needs to return an Object for custom sharing content
Customized sharing field
Sample code
Page({ onShareAppMessage: function () { return { title: '自定义分享标题', path: '/page/user?id=123' } } })
Event handling function
In addition to initialization data and life cycle functions, Page can also define some special functions: events processing function. In the rendering layer, event binding can be added to the component. When the trigger event is reached, the event processing function defined in the Page will be executed.
Sample code:
<view bindtap="viewTap"> click me </view>Page({ viewTap: function() { console.log('view tap') }}) Page.prototype.setData()
The setData function is used to send data from the logic layer to the view layer and change the corresponding value of this.data at the same time.
Note:
Directly modifying this.data is invalid and cannot change the status of the page. It will also cause data inconsistency.
The data set at a time cannot exceed 1024kB. Please try to avoid setting too much data at one time.
setData() parameter format
accepts an object and represents the value corresponding to the key in this.data in the form of key,value Change to value.
The key can be very flexible and given in the form of a data path, such as array[2].message, a.b.c.d, and does not need to be predefined in this.data.
Note:
Directly modifying this.data without calling this.setData will not change the status of the page, and will also cause data inconsistency
The data set at a time cannot exceed 1024kB. Please try to avoid setting too much data at one time.
示例代码:
<!--index.wxml--><view>{{text}}</view><button bindtap="changeText"> Change normal data </button><view>{{num}}</view><button bindtap="changeText"> 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.jsPage({ 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' }) } })
以下内容你不需要立马完全弄明白,不过以后它会有帮助。
生命周期函数
下图说明了Page实例的生命周期。
【相关推荐】
1. 特别推荐:“php程序员工具箱”V0.1版本下载
2. 微信小程序完整源码下载
3. 微信小程序demo:阳淘
The above is the detailed content of page() function for small program development. For more information, please follow other related articles on the PHP Chinese website!