保留当前页面,跳转到应用内的某个页面

Android iOS web
4.11 4.0

# 参数

名称 类型 必填 默认值 兼容性 描述
options NavigateToOptions - - -
名称 类型 必备 默认值 兼容性 描述
url string (string.PageURIString) -
需要跳转的应用内非 tabBar 的页面的路径 , 路径后可以带参数
events any | null -
页面间通信接口,用于监听被打开页面发送到当前页面的数据
success (result: AsyncApiSuccessResult) => void | null -
接口调用成功的回调函数
fail (result: NavigateToFail) => void | null -
接口调用失败的回调函数
complete (result: AsyncApiResult) => void | null -
接口调用结束的回调函数(调用成功、失败都会执行)
名称 类型 必备 默认值 兼容性 描述
errCode number - - 路由错误码 - 4: 框架内部异常
errSubject string - - 统一错误主题(模块)名称
data any | null - - 错误信息中包含的数据
cause Error | null - - 源错误信息,可以包含多个错误,详见SourceError
errMsg string - - -
# AsyncApiResult 的属性值
名称 类型 必备 默认值 兼容性 描述
errMsg string - - -

# 返回值

类型 必备
Promise | null

# 参见

相关 Bug

# uni.redirectTo(options)

关闭当前页面,跳转到应用内的某个页面

# redirectTo 兼容性

Android iOS web
4.11 4.0

# 参数

名称 类型 必填 默认值 兼容性 描述
options RedirectToOptions - - -
名称 类型 必备 默认值 兼容性 描述
url string (string.PageURIString) -
需要跳转的应用内非 tabBar 的页面的路径 , 路径后可以带参数
success (result: AsyncApiSuccessResult) => void | null -
接口调用成功的回调函数
fail (result: RedirectToFail) => void | null -
接口调用失败的回调函数
complete (result: AsyncApiResult) => void | null -
接口调用结束的回调函数(调用成功、失败都会执行)
# RedirectToFail 的属性值
名称 类型 必备 默认值 兼容性 描述
errCode number - - -
errSubject string - - 统一错误主题(模块)名称
data any | null - - 错误信息中包含的数据
cause Error | null - - 源错误信息,可以包含多个错误,详见SourceError
errMsg string - - -
# AsyncApiResult 的属性值
名称 类型 必备 默认值 兼容性 描述
errMsg string - - -

# 返回值

类型 必备
Promise | null

# 参见

相关 Bug

# uni.reLaunch(options)

关闭所有页面,打开到应用内的某个页面

# reLaunch 兼容性

Android iOS web
4.11 4.0

# 参数

名称 类型 必填 默认值 兼容性 描述
options ReLaunchOptions - - -
名称 类型 必备 默认值 兼容性 描述
url string (string.PageURIString) -
需要跳转的应用内页面路径 , 路径后可以带参数。参数与路径之间使用?分隔,参数键与参数值用=相连,不同参数用&分隔;如 'path?key=value&key2=value2',如果跳转的页面路径是 tabBar 页面则不能带参数
success (result: AsyncApiSuccessResult) => void | null -
接口调用成功的回调函数
fail (result: ReLaunchFail) => void | null -
接口调用失败的回调函数
complete (result: AsyncApiResult) => void | null -
接口调用结束的回调函数(调用成功、失败都会执行)
# ReLaunchFail 的属性值
名称 类型 必备 默认值 兼容性 描述
errCode number - - -
errSubject string - - 统一错误主题(模块)名称
data any | null - - 错误信息中包含的数据
cause Error | null - - 源错误信息,可以包含多个错误,详见SourceError
errMsg string - - -
# AsyncApiResult 的属性值
名称 类型 必备 默认值 兼容性 描述
errMsg string - - -

# 返回值

类型 必备
Promise | null

# 参见

相关 Bug

# uni.switchTab(options)

跳转到 tabBar 页面,并关闭其他所有非 tabBar 页面

# switchTab 兼容性

Android iOS web
4.11 4.0

# 参数

名称 类型 必填 默认值 兼容性 描述
options SwitchTabOptions - - -
名称 类型 必备 默认值 兼容性 描述
url string (string.PageURIString) -
需要跳转的 tabBar 页面的路径,路径后不能带参数
success (result: AsyncApiSuccessResult) => void | null -
接口调用成功的回调函数
fail (result: SwitchTabFail) => void | null -
接口调用失败的回调函数
complete (result: AsyncApiResult) => void | null -
接口调用结束的回调函数(调用成功、失败都会执行)
# SwitchTabFail 的属性值
名称 类型 必备 默认值 兼容性 描述
errCode number - - -
errSubject string - - 统一错误主题(模块)名称
data any | null - - 错误信息中包含的数据
cause Error | null - - 源错误信息,可以包含多个错误,详见SourceError
errMsg string - - -
# AsyncApiResult 的属性值
名称 类型 必备 默认值 兼容性 描述
errMsg string - - -

# 返回值

类型 必备
Promise | null

# 参见

相关 Bug

关闭当前页面,返回上一页面或多级页面

Android iOS web
4.11 4.0

# 参数

名称 类型 必填 默认值 兼容性 描述
options NavigateBackOptions | null - -
名称 类型 必备 默认值 兼容性 描述
delta number | null -
返回的页面数,如果 delta 大于现有页面数,则返回到首页
success (result: AsyncApiSuccessResult) => void | null -
接口调用成功的回调函数
fail (result: NavigateBackFail) => void | null -
接口调用失败的回调函数
complete (result: AsyncApiResult) => void | null -
接口调用结束的回调函数(调用成功、失败都会执行)
名称 类型 必备 默认值 兼容性 描述
errCode number - - -
errSubject string - - 统一错误主题(模块)名称
data any | null - - 错误信息中包含的数据
cause Error | null - - 源错误信息,可以包含多个错误,详见SourceError
errMsg string - - -
# AsyncApiResult 的属性值
名称 类型 必备 默认值 兼容性 描述
errMsg string - - -

# 返回值

类型 必备
Promise | null

# 参见

相关 Bug

# 示例

hello uni-app x

Template

Script

<template>
  <!-- #ifdef APP -->
  <scroll-view style="flex: 1">
  <!-- #endif -->
    <view>
      <page-head title="navigate"></page-head>
      <view class="uni-padding-wrap uni-common-mt uni-common-mb">
        <view class="direction-row">
          <text class="label">onLoad触发时间戳:</text>
          <text>{{ onLoadTime }}</text>
        </view>
        <view class="direction-row">
          <text class="label">onShow触发时间戳:</text>
          <text>{{ onShowTime }}</text>
        </view>
        <view class="direction-row">
          <text class="label">onReady触发时间戳:</text>
          <text>{{ onReadyTime }}</text>
        </view>
        <view class="direction-row">
          <text class="label">onHide触发时间戳:</text>
          <text>{{ onHideTime }}</text>
        </view>
        <view class="direction-row">
          <text class="label">onBackPress触发时间戳:</text>
          <text>见控制台</text>
        </view>
        <view class="direction-row">
          <text class="label">onUnload触发时间戳:</text>
          <text>见控制台</text>
        </view>
        <view class="uni-btn-v">
          <button @tap="navigateTo" class="uni-btn">
            跳转新页面,并传递数据
          </button>
          <button @tap="navigateBack" class="uni-btn">返回上一页</button>
          <button @tap="redirectTo" class="uni-btn">在当前页面打开</button>
          <button @tap="switchTab" class="uni-btn">切换到模板选项卡</button>
          <button @tap="reLaunch" class="uni-btn">
            关闭所有页面,打开首页
          </button>
          <button @tap="navigateToErrorPage" class="uni-btn">
            打开不存在的页面
          </button>
        </view>
      </view>
    </view>
  <!-- #ifdef APP -->
  </scroll-view>
  <!-- #endif -->
</template>



<style>
  .direction-row {
    flex-direction: row;
  }

  .label {
    width: 190px;
  }
</style>

# 通用类型

# GeneralCallbackResult

名称 类型 必备 默认值 兼容性 描述
errMsg string - - 错误信息

# 页面跳转与参数传递

A页面跳转到B页面时,有两种方式给B页面传递信息:

  1. A页面跳转时,B页面的URL中通过?param1=param1value&param2=param2value方式传递,然后B页面在Onload里接收参数。详见页面onLoad生命周期
  2. 通过eventbus,详见uni.$on、uni.$emit等API

# Bug & Tips

  • navigateTo, redirectTo 只能打开非 tabBar 页面。
  • switchTab 只能打开 tabBar 页面。
  • reLaunch 可以打开任意页面。
  • 页面底部的 tabBar 由页面决定,即只要是定义为 tabBar 的页面,底部都有 tabBar
  • 不能在首页 onReady 之前进行页面跳转。