Routing

更新时间:
复制 MD 格式

my.switchTab

You can use this API to switch to a specified tab bar page, which closes all other non-tab bar pages.

Note
  • If your miniapp is a multi-tab application, you can use the tab bar configuration items to specify the appearance of the tab bar and the pages that are displayed when a tab is selected.

  • Pages that you navigate to using page navigation (my.navigateTo) or page redirection (my.redirectTo) do not display the bottom tab bar, even if the page is defined in the tab bar configuration.

  • The first page in the tab bar must be the homepage.

For related questions, see Routing FAQ.

Code example

// app.json
{
  "tabBar": {
    "items": [{
      "pagePath": "page/home/index",
      "name": "Homepage"
    },{
      "pagePath": "page/user/index",
      "name": "User"
    }]
  }
}
// .js
my.switchTab({
  url: 'page/home/index'
})

Input parameters

An object with the following properties:

Property

Type

Required

Description

url

String

Yes

The path of the tab bar page to switch to. This page must be defined in the `tabBar` field of `app.json`. Note: You cannot pass parameters in the path.

success

Function

No

The callback function for a successful call.

fail

Function

No

The callback function for a failed call.

complete

Function

No

The callback function that is executed when the call is complete, regardless of whether the call succeeds or fails.

tabBar configuration

Property

Type

Required

Description

textColor

HexColor

No

The text color.

selectedColor

HexColor

No

The color of the selected text.

backgroundColor

HexColor

No

The background color.

items

Array

Yes

The configuration for each tab.

items configuration:

Property

Type

Required

Description

pagePath

String

Yes

Set the page path.

name

String

Yes

The name.

icon

String

No

The path of the normal icon. The recommended size is 60 × 60 px. The system stretches or scales any input image non-proportionally.

activeIcon

String

No

The path of the highlighted icon.

Configuration example

// Example tabBar configuration
{
  "tabBar": {
    "textColor": "#dddddd",
    "selectedColor": "#49a9ee",
    "backgroundColor": "#ffffff",
    "items": [
      {
        "pagePath": "pages/index/index",
        "name": "Homepage"
      },
      {
        "pagePath": "pages/logs/logs",
        "name": "Logs"
      }
    ]
  }
}

my.reLaunch

You can use this API to close all current pages and navigate to a specified page in the application.

Version requirements: Base libraries 1.4.0 or later. If you use an earlier version, we recommend that you perform the steps in compatibility processing.

For related questions, see Routing FAQ.

Code example

my.reLaunch({
  url: '/page/index'
})

Input parameters

An object with the following properties:

Property

Type

Required

Description

url

String

Yes

The page path. If the page is not a tab bar page, you can pass parameters in the path. Parameter rules: Use ? to separate the path from the parameters. Use = to connect a parameter key and its value. Use & to separate different parameters. Example: path?key1=value1&key2=value2

success

Function

No

The callback function for a successful call.

fail

Function

No

The callback function for a failed call.

complete

Function

No

The callback function that is executed when the call is complete, regardless of whether the call succeeds or fails.

my.redirectTo

You can use this API to close the current page and navigate to a specified page in the application.

For related questions, see Routing FAQ.

Note

When you use my.redirectTo to navigate to a page, the current page is closed before the next one is opened. Therefore, the new page does not have a Back button.

Code example

my.redirectTo({
  url: 'new_page?count=100' // The path can be a relative or absolute path.
})

For example, to navigate to the index page:

  • Use an absolute path: The URL is /pages/index/index.

  • Use a relative path: The URL is ../index/index.

Input parameters

The following table describes the properties of the Object.

Property

Type

Required

Description

url

String

Yes

The path of the target page in the application that is not a tab bar page. You can pass parameters in the path. Parameter rules: Use ? to separate the path from the parameters. Use = to connect a parameter key and its value. Use & to separate different parameters. Example: path?key1=value1&key2=value2

success

Function

No

The callback function for a successful call.

fail

Function

No

The callback function for a failed call.

complete

Function

No

The callback function that is executed when the call is complete, regardless of whether the call succeeds or fails.

my.navigateTo

You can use this API to navigate from the current page to a specified page within the application.

  • You can use my.navigateBack to return to the original page.

  • A miniapp can have a page stack of up to ten layers.

You cannot use my.navigateTo or my.redirectTo to navigate to tab bar pages. To navigate to a tab bar page, use my.switchTab.

For more information, see Routing FAQ.

Code example

// API-DEMO page/API/navigator/navigator.json
{
    "defaultTitle": "Page Navigation"
}
<!-- API-DEMO page/API/navigator/navigator.axml-->
<view class="page">
  <view class="page-section">
    <button type="primary" onTap="navigateTo">Navigate to a new page</button>
    <button type="primary" onTap="navigateBack">Return to the previous page</button>
    <button type="primary" onTap="redirectTo">Open in the current page - Get user info</button>
    <button type="primary" onTap="switchTab">Switch to Tab - Component</button>
    <view class="page-description">This demo does not support miniapp navigation. It only shows how to use the API. For specific integration details, see the miniapp-to-miniapp navigation section in the official API documentation.</view>
    <button type="primary" onTap="navigateToMiniProgram">Navigate to a miniapp</button>
    <button type="primary" onTap="navigateBackMiniProgram">Navigate back to a miniapp</button>
  </view>
</view>
// API-DEMO page/API/navigator/navigator.js
Page({
  navigateTo() {
    my.navigateTo({ url: '../get-user-info/get-user-info' })
  },
  navigateBack() {
    my.navigateBack()
  },
  redirectTo() {
    my.redirectTo({ url: '../get-user-info/get-user-info' })
  },
  navigateToMiniProgram() {
    if (my.canIUse('navigateToMiniProgram')) {
      my.navigateToMiniProgram({
        appId: '2017072607907880',
        extraData: {
          "data1": "test"
        },
        success: (res) => {
          console.log(JSON.stringify(res))
        },
        fail: (res) => {
          console.log(JSON.stringify(res))
        }
      });
    }
  },
  navigateBackMiniProgram() {
    if (my.canIUse('navigateBackMiniProgram')) {
      my.navigateBackMiniProgram({
        extraData: {
          "data1": "test"
        },
        success: (res) => {
          console.log(JSON.stringify(res))
        },
        fail: (res) => {
          console.log(JSON.stringify(res))
        }
      });
    }
  },
  switchTab() {
    my.switchTab({
        url: '/page/tabBar/component/index',
        success: () => {
          my.showToast({
            content: 'Success',
            type: 'success',
            duration: 4000
          });
        }
      }
    );
  },
})
/* API-DEMO page/API/navigator/navigator.acss */
button + button {
  margin-top: 20rpx;
}

Input parameters

The following table describes the Object properties.

Property

Type

Required

Description

url

String

Yes

The path of the target page in the application that is not a tab bar page. You can pass parameters in the path. Parameter rules: Use ? to separate the path from the parameters. Use = to connect a parameter key and its value. Use & to separate different parameters. Example: path?key1=value1&key2=value2

success

Function

No

The callback function for a successful call.

fail

Function

No

The callback function for a failed call.

complete

Function

No

The callback function that is executed when the call is complete, regardless of whether the call succeeds or fails.

my.navigateBack

This API closes the current page and returns to a previous page. You can use getCurrentPages to obtain the current page stack and determine how many layers to go back.

For more information, see Routing FAQ.

Example code

// API-DEMO page/API/navigator/navigator.json
{
    "defaultTitle": "Page Navigation"
}
<!-- API-DEMO page/API/navigator/navigator.axml-->
<view class="page">
  <view class="page-section">
    <button type="primary" onTap="navigateTo">Navigate to a new page</button>
    <button type="primary" onTap="navigateBack">Return to the previous page</button>
    <button type="primary" onTap="redirectTo">Open in the current page - Get user info</button>
    <button type="primary" onTap="switchTab">Switch to Tab - Component</button>
    <view class="page-description">This demo does not support miniapp navigation. It only shows how to use the API. For specific integration details, see the miniapp-to-miniapp navigation section in the official API documentation.</view>
    <button type="primary" onTap="navigateToMiniProgram">Navigate to a miniapp</button>
    <button type="primary" onTap="navigateBackMiniProgram">Navigate back to a miniapp</button>
  </view>
</view>
// API-DEMO page/API/navigator/navigator.js
Page({
  navigateTo() {
    my.navigateTo({ url: '../get-user-info/get-user-info' })
  },
  navigateBack() {
    my.navigateBack()
  },
  redirectTo() {
    my.redirectTo({ url: '../get-user-info/get-user-info' })
  },
  navigateToMiniProgram() {
    if (my.canIUse('navigateToMiniProgram')) {
      my.navigateToMiniProgram({
        appId: '2017072607907880',
        extraData: {
          "data1": "test"
        },
        success: (res) => {
          console.log(JSON.stringify(res))
        },
        fail: (res) => {
          console.log(JSON.stringify(res))
        }
      });
    }
  },
  navigateBackMiniProgram() {
    if (my.canIUse('navigateBackMiniProgram')) {
      my.navigateBackMiniProgram({
        extraData: {
          "data1": "test"
        },
        success: (res) => {
          console.log(JSON.stringify(res))
        },
        fail: (res) => {
          console.log(JSON.stringify(res))
        }
      });
    }
  },
  switchTab() {
    my.switchTab({
        url: '/page/tabBar/component/index',
        success: () => {
          my.showToast({
            content: 'Success',
            type: 'success',
            duration: 4000
          });
        }
      }
    );
  },
})
/* API-DEMO page/API/navigator/navigator.acss */
button + button {
  margin-top: 20rpx;
}

Input parameters

An object with the following properties:

Property

Type

Required

Default

Description

delta

Number

No

1

The number of pages to return. If delta is greater than the number of currently open pages, the application returns to the homepage.

Routing FAQ

  • Q: Why does the bottom tab bar not appear for pages navigated to using my.navigateTo or my.redirectTo?

    A: Pages opened with page navigation (my.navigateTo) or page redirection (my.redirectTo) do not display the bottom tab bar, even if the page is defined in the tabBar configuration. To navigate to a tab page, use the my.switchTab method.

  • Q: Does my.navigateTo support passing parameters?

    A: Yes, it does. Parameter rules: Use ? to separate the path from the parameters. Use = to connect a parameter key and its value. Use & to separate different parameters. Example: path?key1=value1&key2=value2

  • Q: Can I remove the back button in the upper-left corner when navigating with my.redirectTo?

    A: When the page stack depth is 1, a page opened with my.redirectTo does not have a back button.

  • Q: Why does my.navigateTo stop working after several navigations?

    A: A miniapp has a maximum page stack of 10 layers. You can use the getCurrentPages method to check the page stack depth. If the limit is exceeded, use page redirection.

  • Q: Can the back button in the miniapp's navigation bar be hidden?

    A: Yes. The back button appears when the page stack has multiple layers. You can call the my.reLaunch method to close all current pages and navigate to the target page. As a result, the back button is not displayed.