registration page


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:

QQ截图20170208095619.png

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.
  },
  // 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: strings, numbers, Boolean values, objects, and arrays.

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 the query in wx.navigateTo and wx.redirectTo and <navigator/>.
  • 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 the page is ready and can Interact with the view layer.
    • Please set interface settings such as wx.setNavigationBarTitle after onReady. See life cycle
  • onHide for details: Page hide

    • whennavigateTo or bottomtabCalled when switching.
  • onUnload: Page unloads

    • when redirectTo or navigateBack when called.

Page related event processing function

  • onPullDownRefresh: Pull-down refresh
    • Listen to user pull-down refresh events .
    • Needs to enable enablePullDownRefresh in the window option of config.
    • After processing the data refresh, wx.stopPullDownRefresh can stop the pull-down refresh of the current page.

Event processing function


In addition to initialization data and life cycle functions, Page can also define some special functions: event processing functions. 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()


setData function is used to set data Sent from the logic layer to the view layer, and at the same time changing the corresponding value of this.data .

Note:

  1. Directly modifying this.data is invalid and cannot change the status of the page. It will also cause data inconsistency.
  2. 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, in the form of key and value, to change the value corresponding to the key in this.data 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 in this.data Defined in advance.

Sample code:

<!--index.wxml-->
<view>{{text}}</view>
<button bindtap="changeText"> Change normal data </button>
<view>{{array[0].text}}</view>
<button bindtap="changeItemInArray"> Change Array data </button>
<view>{{obj.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',
    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'
    })
  }
})

getCurrentPages()


##getCurrentPages() Function Used to get the instance of the current page stack, given in the form of an array in the order of the stack. The first element is the home page, and the last element is the current page.

Note: Do not try to modify the page stack, as it will cause routing and page status errors.

Page stack

The framework maintains all current pages in the form of a stack. When a routing switch occurs, the page stack behaves as follows:

QQ截图20170208095642.png

#You don’t need to fully understand the following content immediately, but it will be explained later will help.

Life cycle function


The following figure illustrates the life cycle of the Page instance

3.png

Page routing

The routing of all pages in the mini program is managed by the framework. The triggering method of routing and the page life cycle function are as follows:


QQ截图20170208095659.png

Tab switching corresponding life cycle (take pages A and B as Tabbar pages, C is the page opened from page A, and page D is the page opened from page C):

QQ截图20170208095715.png

Bug & Tip

  1. bug: iOS/Android 6.3.30 , when entering the page for the first time, onReachBottom will be triggered if the page is not full of one screen. It should be triggered only when the user actively pulls up;
  2. bug: iOS/Android 6.3.30, when the finger is pulled up, onReachBottom will be triggered multiple times. It should be pulled up once and only triggered once;