Registration page of WeChat applet tutorial

Source: Internet
Author: User
The Page () function is used to register a Page. An object parameter is used to specify the initial data, lifecycle function, and event processing function of the page. Applet-Page

The Page () function is used to register a Page. An object parameter is used to specify the initial data, lifecycle function, and event processing function of the page.

Object parameter description:

Attribute type description

Initial data on the data Object page

OnLoad Function lifecycle Function-listener page loading

OnReady Function lifecycle Function-listener page rendering complete

OnShow Function lifecycle Function -- display on the listener page

OnHide Function lifecycle Function-listener page hiding

OnUnload Function lifecycle Function -- uninstall the listener page

Other Any developers can add Any function or data to the Object parameter, and use this to access

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.   },   // Event handler.   viewTap: function() {   this.setData({    text: 'Set some data for updating view.'   })   }  })

Initialize data

The initialization data is used as the first page rendering. Data is transmitted from the logic layer to the rendering layer in JSON format. Therefore, the data must be in JSON format: string, number, Boolean value, object, and array.

The rendering layer can bind data through WXML.

Sample code:

 
  {{text}}
   
 
  {{array[0].msg}}
 
Page({   data: {   text: 'init data',   array: [{msg: '1'}, {msg: '2'}]   }  })

Event Processing Functions

In addition to initializing data and Lifecycle Functions, Page can also define some special functions: event processing functions. You can add event binding to the rendering layer. When an event is triggered, the event processing function defined in Page is executed.

Sample code:

Click me

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 value of this. data.

Note:



Modifying this. data directly is invalid, and the page status cannot be changed, resulting in data inconsistency.

The data set at a time cannot exceed KB. avoid setting too much data at a time.



SetData () parameter format

Accept an object and change the value corresponding to the key in this. data to value in the form of key and value.

The key can be very flexible and provided in the form of data paths, such as array [2]. message, a. B. c. d, and does not need to be pre-defined in this. data.

Sample code:

   
 
  {{text}}
    Change normal data   
 
  {{array[0].text}}
    Change Array data   
 
  {{obj.text}}
    Change Object data   
 
  {{newField.text}}
    Add new data 
//index.js  Page({   data: {   text: 'init data',   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'   })   },   changeItemInArray: function() {   // you can use this way to modify a danamic data path   var changedData = {}   var index = 0   changedData['array[' + index + '].text'] = 'changed data'   this.setData(changedData)   },   changeItemInObject: function(){   this.setData({    'object.text': 'changed data'   });   },   addNewField: function() {   this.setData({    'newField.text': 'new data'   })   }

You do not need to fully understand the following content, but it will be helpful in the future.

Lifecycle functions

Describes the lifecycle of a Page instance.

The above is the registration page of the mini-program tutorial. For more information, see The PHP Chinese website (www.php.cn )!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.