注册页面

对于小程序中的每个页面,都需要在页面对应的js文件中进行注册,指定页面的初始数据、生命周期回调、事件处理函数等。

使用 Page 构造器注册页面

简单的页面可以使用Page()进行构造。
代码示例:

  1. //index.js
  2. Page({
  3. data: {
  4. text: "This is page data."
  5. },
  6. onLoad: function(options) {
  7. // 页面创建时执行
  8. },
  9. onShow: function() {
  10. // 页面出现在前台时执行
  11. },
  12. onReady: function() {
  13. // 页面首次渲染完毕时执行
  14. },
  15. onHide: function() {
  16. // 页面从前台变为后台时执行
  17. },
  18. onUnload: function() {
  19. // 页面销毁时执行
  20. },
  21. onPullDownRefresh: function() {
  22. // 触发下拉刷新时执行
  23. },
  24. onReachBottom: function() {
  25. // 页面触底时执行
  26. },
  27. onShareAppMessage: function () {
  28. // 页面被用户分享时执行
  29. },
  30. onPageScroll: function() {
  31. // 页面滚动时执行
  32. },
  33. onResize: function() {
  34. // 页面尺寸变化时执行
  35. },
  36. onTabItemTap(item) {
  37. // tab 点击时执行
  38. console.log(item.index)
  39. console.log(item.pagePath)
  40. console.log(item.text)
  41. },
  42. // 事件响应函数
  43. viewTap: function() {
  44. this.setData({
  45. text: 'Set some data for updating view.'
  46. }, function() {
  47. // this is setData callback
  48. })
  49. },
  50. // 自由数据
  51. customData: {
  52. hi: 'MINA'
  53. }
  54. })