# Registration page

For each page in the Mini Program, you need to be on the page corresponding js File, specifying the page's initial data, lifecycle callbacks, event handlers, and so on.

# use Page Constructor Registration Page

Simple pages can be used Page() For construction.

Code example:

//index.js
Page({
  data: {
    text: "This is Page data."
  },
  onLoad: function(options) {
    // Executed when the page is created
  },
  onShow: function() {
    // Executed when the page appears in the foreground
  },
  onReady: function() {
    // When the page is first rendered
  },
  onHide: function() {
    // Executes when the page changes from foreground to background
  },
  onUnload: function() {
    // When the page is destroyed
  },
  onPullDownRefresh: function() {
    // Executes when a drop-down refresh is triggered
  },
  onReachBottom: function() {
    // Execute when the page hits bottom
  },
  onShareAppMessage:  function () {
    // Executed when the page is shared by the user
  },
  onPageScroll: function() {
    // Performed when page scrolling
  },
  onResize: function() {
    // When page size changes
  },
  onTabItemTap(item) {
    // tab Execute when clicked
    console.log(item.index)
    console.log(item.pagePath)
    console.log(item.text)
  },
  // Event response function
  viewTap: function() {
    this.setData({
      text: 'Set Some Data for updating view.'
    }, function() {
      // this is setData callback
    })
  },
  // Free data
  customData: {
    hi: 'MINA'
  }
})

Please refer to the detailed meaning and use of parameters Page Reference documents

# Use in the page behaviors

Start from base library version 2.9.2. Please remaining backward compatible.

Pages can be referenced 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 Specific Usage behaviors

# use Component The constructor constructs the page

Start from base library version 1.6.3. Please remaining backward compatible.

Page The constructor works for simple pages. But for complex pages, Page The constructor may not be useful.

At this point, you can use Component Constructor to construct the page. Component The main difference between a constructor is that the method needs to be placed in the methods: { } Inside.

Code example:

Component({
  data: {
    text: "This is Page data."
  },
  methods: {
    onLoad: function(options) {
      // Executed when the page is created
    },
    onPullDownRefresh: function() {
      // Perform when a drop-down refresh
    },
    // Event response function
    viewTap: function() {
      // ...
    }
  }
})

This way of creating is very similar to Custom Components , can be used like a custom component behaviors Other advanced features.

Please read the details Component Constructor Chapters.