Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

WeChat program registration page


May 17, 2021 WeChat Mini Program Development Document


Table of contents


The registration page

For each page in the small program, registration is required in the js file corresponding to the page, specifying the initial data of the page, lifecycle callbacks, event handlers, and so on.

Register the page with the Page constructor

Simple pages can be constructed using Page().

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'
  }
})

Refer to the Page reference document for detailed parameter meaning and use.

Use behaviors on the page

Base library 2.9.2 starts to support, and low versions need to be compatible.

The page can refer to 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.'
  }
})

See behaviors for specific usage.

Use the Component constructor to construct a page

Base library 1.6.3 starts to support, and low versions need to be compatible.

The Page constructor is suitable for simple pages. But for complex pages, the Page constructor may not work well.

At this point, you can use the Component constructor to construct the page. The main difference with the Component constructor is that the method needs to be placed in the methods: .

Code example:

Component({
  data: {
    text: "This is page data."
  },
  methods: {
    onLoad: function(options) {
      // 页面创建时执行
    },
    onPullDownRefresh: function() {
      // 下拉刷新时执行
    },
    // 事件响应函数
    viewTap: function() {
      // ...
    }
  }
})

This is created much like a custom component, and you can use advanced features such as behaviors just like custom components.

For more details, read the Component Constructor section.