The“Page”for page display and interactionrepresents a page of the application. Each page corresponds to a subdirectory. Generally, the number of pages is as many as subdirectories. It is also a constructor forgenerating page instances.

Page initialization

When the page is initialized, you need to provide data as the first rendering of the page:

  1. <view>{{title}}</view>
  2. <view>{{array[0].user}}</view>
  1. Page({
  2. data: {
  3. title: 'Alipay',
  4. array: [{
  5. user: 'li'
  6. },
  7. {
  8. user: 'zhao'
  9. }]
  10. }
  11. })

When defining the interaction behavior, you need to specify the response function in the page script:

  1. <view onTap="handleTap">click me</view>

The handleTap method is called when users click the above template:

  1. Page({
  2. handleTap() {
  3. console.log('yo! view tap!')
  4. }
  5. })

To re-render the page, you need to call “this.setData” method in the page script.

  1. <view>{{text}}</view>
  2. <button onTap="changeText"> Change normal data </button>

The “changeText” method is called whenspecified users of the above code touch the button,

  1. Page({
  2. data: {
  3. text: 'init data',
  4. },
  5. changeText() {
  6. this.setData({
  7. text: 'changed data'
  8. })
  9. },
  10. })

In the above code, the calling of “this.setData” in the “changeText” method will cause the re-renderingof pages.

Page()

Page() accepts an “object” as a parameter, which is used to specify the initial data, lifecycle function, event processing function and others of the page.

  1. //index.js
  2. Page({
  3. data: {
  4. title: "Alipay"
  5. },
  6. onLoad(query) {
  7. // 页面加载
  8. },
  9. onReady() {
  10. // 页面加载完成
  11. },
  12. onShow() {
  13. // 页面显示
  14. },
  15. onHide() {
  16. // 页面隐藏
  17. },
  18. onUnload() {
  19. // 页面被关闭
  20. },
  21. onTitleClick() {
  22. // 标题被点击
  23. },
  24. onPullDownRefresh() {
  25. // 页面被下拉
  26. },
  27. onReachBottom() {
  28. // 页面被拉到底部
  29. },
  30. onShareAppMessage() {
  31. // 返回自定义分享信息
  32. },
  33. viewTap() {
  34. // 事件处理
  35. this.setData({
  36. text: 'Set data for updat.'
  37. })
  38. },
  39. go() {
  40. // 带参数的跳转,从 page/index 的 onLoad 函数的 query 中读取 xx
  41. my.navigateTo('/page/index?xx=1')
  42. },
  43. customData: {
  44. hi: 'alipay'
  45. }
  46. })

In the above code, the parameter object of the “Page()” method is described as follows:

Attribute Type Description
data Object or Function Initial data or function that returns initialized data
onTitleClick Function Click on the title for triggering
onOptionMenuClick Function It is supported by basic library 1.3.0+ . click on the extra navigation bar icon for triggering, which can be judged bymy.canIUse(‘page.onOptionM enuClick’)
onPageScroll Function({scrollTop}) Triggered when the page scrolls
onLoad Function(query: Object) Triggered when the page scrolls
onReady Function Page is triggered when the first rendering is completed
onShow Function Triggered when the page is displayed
onHide Function Triggered when the page is hidden
onUnload Function Triggered when the page is unloaded
onPullDownRefresh Function Triggered when the page is pulled down
onReachBottom Function Triggered when it’s pulled up to the bottom
onShareAppMessage Function Triggered when you click on the top right for sharing
other Any Developers can add any function or attribute to the “object” parameter,which can be accessed in the function of the page with “this”.

Note: If you modify “data” in the page, different instances of the page will be affected when “data” is an object.

Life cycle method

  • onLoad:page loading. One page will only be called once, and the “query” parameters are the “query” objectpassed in“my.navigateTo” and “my.redirectTo”.
  • onShow:page display. It will be called once every time the page is displayed.
  • onReady:The first rendering of the page is completed. A page will only be called once, which means that the page is ready to interact with the presentation layer. For the setting of the interface like“my.setNavigationBar”, please set it after “onReady”.
  • onHide:page hiding. It is called when you use“my.navigateTo” to switch to other pages or use “tab” at the bottom for switching.
  • onUnload:page unloading. It is called when you use“my.redirectTo” or “my.navigateBack” to switch to other pages.

Event handlers

  • onPullDownRefresh:Pull down to refresh. To monitor users in pull-downrefresh, “pullRefresh” needs to be opened in the window option of app.json. After processing the data refresh, “my.stopPullDownRefresh” can stop the pull-down refresh of the current page.
  • onShareAppMessage:user sharing.

Page.prototype.setData()

The “setData” function is used to send data from the logical layer to the presentation layer, changing the corresponding value of “this.data” in the meantime.

Note:

  • It’s invalid to modify “this.data” directly with failure to change the status of the page, causing inconsistent data as well.
  • Please try to avoid setting too many data once.

“setData”receives an object as a parameter. The “key” nameof the object given in the form of data path can be very flexible, such as “array[2].message, a.b.c.d”, and does not need to be predefined in “this.data”.

Code example

  1. <view>{{text}}</view>
  2. <button onTap="changeTitle"> Change normal data </button>
  3. <view>{{array[0].text}}</view>
  4. <button onTap="changeArray"> Change Array data </button>
  5. <view>{{object.text}}</view>
  6. <button onTap="changePlanetColor"> Change Object data </button>
  7. <view>{{newField.text}}</view>
  8. <button onTap="addNewKey"> Add new data </button>
  1. Page({
  2. data: {
  3. text: 'test',
  4. array: [{
  5. text: 'a'
  6. }],
  7. object: {
  8. text: 'blue'
  9. }
  10. },
  11. changeTitle() {
  12. // 错误!不要直接去修改 data 里的数据
  13. // this.data.text = 'changed data'
  14. // 正确
  15. this.setData({
  16. text: 'ha'
  17. })
  18. },
  19. changeArray() {
  20. // 可以直接使用数据路径来修改数据
  21. this.setData({
  22. 'array[0].text': 'b'
  23. })
  24. },
  25. changePlanetColor() {
  26. this.setData({
  27. 'object.text': 'red'
  28. });
  29. },
  30. addNewKey() {
  31. this.setData({
  32. 'newField.text': 'c'
  33. })
  34. }
  35. })

getCurrentPages()

The “getCurrentPages()” function is used to obtain the instance of the current page stack, which is given in stack order in the form of array. The first element is the homepage, and the last element is the current page. The following code can be used to detect whether there’s depth of 5 levels in the current page.

  1. if (getCurrentPages().length === 5) {
  2. my.redirectTo('/xx');
  3. } else {
  4. my.navigateTo('/xx');
  5. }

Note: Don’t try to modify the page stack, otherwise it will cause routing and page status errors.

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

Routing mode Page stack performance
Initialization New page stack
Open new page New page stack
Page redirection The current page pops stack, and the new page pushes stack
Page back The current page pops the stack
Tab switch All pages pops the stack, leaving only a new “Tab” page

page.json

The “[page name].json” filecan also be used ineach pageto configure the window performance of the current page.

The configuration of the page is much simpler than the global configuration of “app.json”. Only window-related configuration items can be set, so there is no need to write the “window” key. Please note that the page configuration will override the configuration items in the window attribute of “app.json”.

It especially supports navigation icon of “optionMenu” configuration. And click it to trigger “onOptionMenuClick”.

File Type Required Description
optionMenu Object No Basic library 1.3.0+. setting an extra icon in the navigation bar.
Supporting setting attribute “icon” for now, the value is icon URL (starting with https/http) or base64 string. 30*30px is recommended

例如:

  1. {
  2. "optionMenu": {
  3. "icon": "https://img.alicdn.com/tps/i3/T1OjaVFl4dXXa.JOZB-114-114.png"
  4. }
  5. }

page style

The root element in each page is “page”.You can use this element when you need to set the height or background color.

  1. page {
  2. background - color: #fff;
  3. }