目录

Page

In the ASCF framework, you can register a page using Page or register a page using Component.

Page

Page(Object object)

Since: 1.0.0

Parameters

The input parameter is an object containing the following fields.

Attribute Type Mandatory Description
data object No Initial data of the page.
options object No Component options of the page, which are the same as those in Component.
onLoad function No Lifecycle callback, used to listen for page loading.
onShow function No Lifecycle callback, used to listen for page display.
onReady function No Lifecycle callback, used to listen for initial rendering of the page.
onHide function No Lifecycle callback, used to listen for page hiding.
onUnload function No Lifecycle callback, used to listen for page unloading.
onPullDownRefresh function No A function that listens for the pull-down action from a user.
onReachBottom function No A function that processes the pull-up event on a page.
onPageScroll function No A function that processes the scrolling event on a page.
onTabItemTap function No A function triggered when a tab is tapped.
getTabBar function No

A function used to obtain the custom tabBar instance.

Since: 1.0.10

onShareAppMessage function No

A function triggered when a user taps the menu bar in the upper right corner.

Since: 1.0.16

Others any No A parameter that adds any function or data to a parameter of the Object type and uses this to access the parameter.

[h2]onLoad

onLoad(Object query)

Called when a page is loaded. This function is called only once for one page. You can obtain the parameters of the loaded page's path from onLoad.

Since: 1.0.0

Parameters

Name Type Mandatory Description
query Object No

Parameter obtained from the URL field of the has.navigateTo and has.redirectTo APIs, for example, path?key1=value1&key2=value2.

The base library parses the string content into an Object.

[h2]onPageScroll

onPageScroll(Object object)

Called when a page is scrolled.

Since: 1.0.0

Parameters

The input parameter is an object containing the following fields.

Attribute Type Mandatory Description
scrollTop Number No The vertical scrolling distance of the page, in px.

[h2]onTabItemTap

onTabItemTap(Object item)

Called when a tab is tapped.

Since: 1.0.0

Parameters

The input parameter is an object containing the following fields.

Parameter Type Mandatory Description
index String No Sequence number of the tapped tab item, starting from 0.
pagePath String No Page path of the tapped tab item.
text String No Button text of the tapped tab item.

Example:

Page({
  onTabItemTap(item) {
    console.info(item.index);
    console.info(item.pagePath);
    console.info(item.text);
  }
});

[h2]getTabBar

getTabBar()

Obtains the custom tabBar instance.

Since: 1.0.10

Example:

Page({
  onShow() {
    if (typeof this.getTabBar === 'function' && this.getTabBar()) {
      this.getTabBar().setData({ selected: 1 });
    }
  }
});

[h2]onShareAppMessage

onShareAppMessage(Object object)

Called when a user taps the share button (open-type="share" of the button component) on the page or taps the menu bar in the upper right corner, and customizes the content to be shared.

If the ROM version is earlier than 7.0.0, this function is called when the user taps the menu bar in the upper right corner. If the ROM version is 7.0.0 or later, this function is called when the user taps the share button on the service panel.

Since: 1.0.16

Precautions: The system sync API cannot be called in onShareAppMessage.

Dependency: HarmonyOS SDK version 6.0.0(20) or later and ROM version 6.0.0 or later

Parameters

The input parameter is an object containing the following fields.

Parameter Type Mandatory Description
webViewUrl String No URL of the web-view component.

Returns

An object containing the following system information fields.

Name Type Default Value Description
title string The default value is the current atomic service name. Title of the content to be shared
path string Path of the current page. The value must be a complete path starting with a slash (/). Path to be shared

Example:

Page({
  onShareAppMessage() {
    return {
      title: 'Custom title',
      path: '/page/user?id=123'
    };
  }
});

[h2]Registering a Page Using Page

// index.js
Page({
  data: {
    text: 'This is page data.'
  },
  onLoad: function(options) {
    // Executed when a page is created.
  },
  onShow: function() {
    // Executed when the page is displayed.
  },
  onReady: function() {
    // Executed when the page is rendered for the first time.
  },
  onHide: function() {
    // Executed when the page is hidden.
  },
  onUnload: function() {
    // Executed when the page is unloaded.
  },
  onPullDownRefresh: function() {
    // Executed upon a pull-down refresh.
  },
  onReachBottom: function() {
    // Executed when the page reaches the bottom.
  },
  onPageScroll: function() {
    // Executed when the page is scrolling.
  },
  onTabItemTap(item) {
    // Executed when a tab is tapped.
  },
  // Event callback.
  handleTap: function() {
    this.setData(
      {
        text: 'new text'
      },
      function() {
        // Triggered after setData is updated.
      }
    );
  },
  // Other custom data
  extraData: {
    hi: 'extra data'
  }
});

[h2]Registering a Page Using Component

Use Component() to construct pages. Various features of components are available, such as behaviors.

Custom methods and page lifecycles must be defined in methods.

Component({
  data: {
    text: 'page data'
  },
  methods: {
    onLoad: function(options) {
      // Executed when a page is created.
    },
    onPullDownRefresh: function() {
      // Executed upon a pull-down refresh.
    },
    // Event response function
    handleTap: function() {
      // ...
    }
  }
});

getCurrentPages

getCurrentPages()

Obtains the current page stack. The last element in the array is the current page.

  • Do not modify the page stack. Otherwise, the page route will be disordered.
  • Do not call getCurrentPages() before calling App.onLaunch or initial App.onShow because the page stack has not been generated yet.

Since: 1.0.0