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

# # Parameters

name type required default description
options NavigateToOptions YES - -
name type optinal default description
url string (string.PageURIString) YES - 需要跳转的应用内非 tabBar 的页面的路径 , 路径后可以带参数
success (result: AsyncApiSuccessResult) => void NO - 接口调用成功的回调函数
fail (result: UniError) => void NO - 接口调用失败的回调函数
complete (result: AsyncApiResult) => void NO - 接口调用结束的回调函数(调用成功、失败都会执行)
# # AsyncApiSuccessResult Values
name type optinal default description
errMsg string YES - -
# # AsyncApiResult Values
name type optinal default description
errMsg string YES - -
Android version Android uni-app Android uni-app-x iOS version iOS uni-app iOS uni-app-x
url 5.0 10.0 x
success 5.0 10.0 x
fail 5.0 10.0 x
complete 5.0 10.0 x

# # Return value

Type optinal
Promise<AsyncApiSuccessResult> NO

# # AsyncApiSuccessResult Values

name type optinal default description
errMsg string YES - -
Android version Android uni-app Android uni-app-x iOS version iOS uni-app iOS uni-app-x
5.0 10.0 x

# # See also

navigateTo

Related Bug

# uni.redirectTo(options)

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

# # Parameters

name type required default description
options RedirectToOptions YES - -

# # RedirectToOptions Values

name type optinal default description
url string (string.PageURIString) YES - 需要跳转的应用内非 tabBar 的页面的路径 , 路径后可以带参数
success (result: AsyncApiSuccessResult) => void NO - 接口调用成功的回调函数
fail (result: UniError) => void NO - 接口调用失败的回调函数
complete (result: AsyncApiResult) => void NO - 接口调用结束的回调函数(调用成功、失败都会执行)
# # AsyncApiResult Values
name type optinal default description
errMsg string YES - -
# # RedirectToOptions Compatibility
Android version Android uni-app Android uni-app-x iOS version iOS uni-app iOS uni-app-x
url 5.0 10.0 x
success 5.0 10.0 x
fail 5.0 10.0 x
complete 5.0 10.0 x

# # Return value

Type optinal
Promise<AsyncApiSuccessResult> NO

# # AsyncApiSuccessResult Values

name type optinal default description
errMsg string YES - -

# # redirectTo Compatibility

Android version Android uni-app Android uni-app-x iOS version iOS uni-app iOS uni-app-x
5.0 10.0 x

# # See also

redirectTo

Related Bug

# uni.reLaunch(options)

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

# # Parameters

name type required default description
options ReLaunchOptions YES - -

# # ReLaunchOptions Values

name type optinal default description
url string (string.PageURIString) YES - 需要跳转的应用内非 tabBar 的页面的路径 , 路径后可以带参数
success (result: AsyncApiSuccessResult) => void NO - 接口调用成功的回调函数
fail (result: UniError) => void NO - 接口调用失败的回调函数
complete (result: AsyncApiResult) => void NO - 接口调用结束的回调函数(调用成功、失败都会执行)
# # AsyncApiResult Values
name type optinal default description
errMsg string YES - -
# # ReLaunchOptions Compatibility
Android version Android uni-app Android uni-app-x iOS version iOS uni-app iOS uni-app-x
url 5.0 10.0 x
success 5.0 10.0 x
fail 5.0 10.0 x
complete 5.0 10.0 x

# # Return value

Type optinal
Promise<AsyncApiSuccessResult> NO

# # AsyncApiSuccessResult Values

name type optinal default description
errMsg string YES - -

# # reLaunch Compatibility

Android version Android uni-app Android uni-app-x iOS version iOS uni-app iOS uni-app-x
5.0 10.0 x

# # See also

reLaunch

Related Bug

# uni.switchTab(options)

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

# # Parameters

name type required default description
options SwitchTabOptions YES - -

# # SwitchTabOptions Values

name type optinal default description
url string (string.PageURIString) YES - 需要跳转的 tabBar 页面的路径,路径后不能带参数
success (result: AsyncApiSuccessResult) => void NO - 接口调用成功的回调函数
fail (result: UniError) => void NO - 接口调用失败的回调函数
complete (result: AsyncApiResult) => void NO - 接口调用结束的回调函数(调用成功、失败都会执行)
# # AsyncApiResult Values
name type optinal default description
errMsg string YES - -
# # SwitchTabOptions Compatibility
Android version Android uni-app Android uni-app-x iOS version iOS uni-app iOS uni-app-x
url 5.0 10.0 x
success 5.0 10.0 x
fail 5.0 10.0 x
complete 5.0 10.0 x

# # Return value

Type optinal
Promise<AsyncApiSuccessResult> NO

# # AsyncApiSuccessResult Values

name type optinal default description
errMsg string YES - -

# # switchTab Compatibility

Android version Android uni-app Android uni-app-x iOS version iOS uni-app iOS uni-app-x
5.0 10.0 x

# # See also

switchTab

Related Bug

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

# # Parameters

name type required default description
options NavigateBackOptions NO - -
name type optinal default description
delta number NO - 返回的页面数,如果 delta 大于现有页面数,则返回到首页
success (result: AsyncApiSuccessResult) => void NO - 接口调用成功的回调函数
fail (result: UniError) => void NO - 接口调用失败的回调函数
complete (result: AsyncApiResult) => void NO - 接口调用结束的回调函数(调用成功、失败都会执行)
# # AsyncApiResult Values
name type optinal default description
errMsg string YES - -
Android version Android uni-app Android uni-app-x iOS version iOS uni-app iOS uni-app-x
delta 5.0 10.0 x
success 5.0 10.0 x
fail 5.0 10.0 x
complete 5.0 10.0 x

# # Return value

Type optinal
Promise<AsyncApiSuccessResult> NO

# # AsyncApiSuccessResult Values

name type optinal default description
errMsg string YES - -
Android version Android uni-app Android uni-app-x iOS version iOS uni-app iOS uni-app-x
5.0 10.0 x

# # See also

navigateBack

Related Bug

# # Example

hello uni-app x

<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>

<script lang="uts">
import { State, state, setLifeCycleNum } from '@/store/index.uts'

export default {
 data() {
   return {
     onLoadTime: 0,
     onShowTime: 0,
     onReadyTime: 0,
     onHideTime: 0,
   }
 },
 onLoad() {
   this.onLoadTime = Date.now()
   console.log('onLoad', this.onLoadTime)
 },
 onShow() {
   this.onShowTime = Date.now()
   console.log('onShow', this.onShowTime)
 },
 onReady() {
   this.onReadyTime = Date.now()
   console.log('onReady', this.onReadyTime)
 },
 onHide() {
   this.onHideTime = Date.now()
   console.log('onHide', this.onHideTime)
 },
 onBackPress(options: OnBackPressOptions): boolean | null {
   console.log('onBackPress', Date.now())
   console.log('onBackPress from', options.from)
   return null
 },
 onUnload() {
   console.log('onUnload', Date.now())
 },
 methods: {
   reLaunch() {
     uni.reLaunch({
       url: '/pages/tabBar/component',
       success(result) {
         console.log('reLaunch success', result.errMsg)
         // 自动化测试
         setLifeCycleNum(state.lifeCycleNum + 1)
       },
       fail(error) {
         console.log('reLaunch fail', error.errMsg)
         // 自动化测试
         setLifeCycleNum(state.lifeCycleNum - 1)
       },
       complete(result) {
         console.log('reLaunch complete', result.errMsg)
         // 自动化测试
         setLifeCycleNum(state.lifeCycleNum + 1)
       },
     })
   },
   navigateTo() {
     uni.navigateTo({
       url: '/pages/API/navigator/new-page/new-page-1?data=Hello',
       success(result) {
         console.log('navigateTo success', result.errMsg)
         // 自动化测试
         setLifeCycleNum(state.lifeCycleNum + 1)
       },
       fail(error) {
         console.log('navigateTo fail', error.errMsg)
         // 自动化测试
         setLifeCycleNum(state.lifeCycleNum - 1)
       },
       complete(result) {
         console.log('navigateTo complete', result.errMsg)
         // 自动化测试
         setLifeCycleNum(state.lifeCycleNum + 1)
       },
     })
   },
   navigateToErrorPage() {
     uni.navigateTo({
       url: '/pages/error-page/error-page',
       success(result) {
         console.log('navigateTo success', result.errMsg)
         // 自动化测试
         setLifeCycleNum(state.lifeCycleNum - 1)
       },
       fail(error) {
         console.log('navigateTo fail', error.errMsg)
         uni.showToast({
           title: error.errMsg,
           icon: 'none',
         })
         // 自动化测试
         setLifeCycleNum(state.lifeCycleNum + 1)
       },
       complete(result) {
         console.log('navigateTo complete', result.errMsg)
         // 自动化测试
         setLifeCycleNum(state.lifeCycleNum + 1)
       },
     })
   },
   navigateToDebounce() {
     uni.navigateTo({
       url: '/pages/API/navigator/new-page/new-page-1?data=debounce',
       success(result) {
         console.log('navigateTo success', result.errMsg)
         // 自动化测试
         setLifeCycleNum(state.lifeCycleNum + 1)
       },
       fail(error) {
         console.log('navigateTo fail', error.errMsg)
         // 自动化测试
         setLifeCycleNum(state.lifeCycleNum - 1)
       },
       complete(result) {
         console.log('navigateTo complete', result.errMsg)
         // 自动化测试
         setLifeCycleNum(state.lifeCycleNum + 1)
       },
     })
     uni.navigateTo({
       url: '/pages/API/navigator/new-page/new-page-1?data=debounce',
       success(result) {
         console.log('navigateTo success', result.errMsg)
         // 自动化测试
         setLifeCycleNum(state.lifeCycleNum - 1)
       },
       fail(error) {
         console.log('navigateTo fail', error.errMsg)
         // 自动化测试
         setLifeCycleNum(state.lifeCycleNum + 1)
       },
       complete(result) {
         console.log('navigateTo complete', result.errMsg)
         // 自动化测试
         setLifeCycleNum(state.lifeCycleNum + 1)
       },
     })
   },
   // 自动化测试
   navigateToRelativePath1() {
     uni.navigateTo({
       url: 'new-page/new-page-1?data=new-page/new-page-1',
       success() {
         setLifeCycleNum(state.lifeCycleNum + 1)
       },
       fail() {
         setLifeCycleNum(state.lifeCycleNum - 1)
       },
       complete() {
         setLifeCycleNum(state.lifeCycleNum + 1)
       },
     })
   },
   // 自动化测试
   navigateToRelativePath2() {
     uni.navigateTo({
       url: './new-page/new-page-1?data=./new-page/new-page-1',
       success() {
         setLifeCycleNum(state.lifeCycleNum + 1)
       },
       fail() {
         setLifeCycleNum(state.lifeCycleNum - 1)
       },
       complete() {
         setLifeCycleNum(state.lifeCycleNum + 1)
       },
     })
   },
   // 自动化测试
   navigateToRelativePath3() {
     uni.navigateTo({
       url: '../navigator/new-page/new-page-1?data=../navigator/new-page/new-page-1',
       success() {
         setLifeCycleNum(state.lifeCycleNum + 1)
       },
       fail() {
         setLifeCycleNum(state.lifeCycleNum - 1)
       },
       complete() {
         setLifeCycleNum(state.lifeCycleNum + 1)
       },
     })
   },
   navigateBack() {
     uni.navigateBack({
       success(result) {
         console.log('navigateBack success', result.errMsg)
         // 自动化测试
         setLifeCycleNum(state.lifeCycleNum + 1)
       },
       fail(error) {
         console.log('navigateBack fail', error.errMsg)
         // 自动化测试
         setLifeCycleNum(state.lifeCycleNum - 1)
       },
       complete(result) {
         console.log('navigateBack complete', result.errMsg)
         // 自动化测试
         setLifeCycleNum(state.lifeCycleNum + 1)
       },
     })
   },
   navigateBackWithDelta1() {
     uni.navigateTo({
       url: '/pages/API/navigator/new-page/new-page-1',
       success() {
         uni.navigateBack({
           delta: 1,
           success(result) {
             console.log('navigateBack success', result.errMsg)
             // 自动化测试
             setLifeCycleNum(state.lifeCycleNum + 1)
           },
           fail(error) {
             console.log('navigateBack fail', error.errMsg)
             // 自动化测试
             setLifeCycleNum(state.lifeCycleNum - 1)
           },
           complete(result) {
             console.log('navigateBack complete', result.errMsg)
             // 自动化测试
             setLifeCycleNum(state.lifeCycleNum + 1)
           },
         })
       },
     })
   },
   navigateBackWithDelta100() {
     uni.navigateTo({
       url: '/pages/API/navigator/new-page/new-page-1',
       success() {
         uni.navigateBack({
           delta: 100,
           success(result) {
             console.log('navigateBack success', result.errMsg)
             // 自动化测试
             setLifeCycleNum(state.lifeCycleNum + 1)
           },
           fail(error) {
             console.log('navigateBack fail', error.errMsg)
             // 自动化测试
             setLifeCycleNum(state.lifeCycleNum - 1)
           },
           complete(result) {
             console.log('navigateBack complete', result.errMsg)
             // 自动化测试
             setLifeCycleNum(state.lifeCycleNum + 1)
           },
         })
       },
     })
   },
   redirectTo() {
     uni.redirectTo({
       url: '/pages/API/navigator/new-page/new-page-1?data=Hello',
       success(result) {
         console.log('redirectTo success', result.errMsg)
         // 自动化测试
         setLifeCycleNum(state.lifeCycleNum + 1)
       },
       fail(error) {
         console.log('redirectTo fail', error.errMsg)
         // 自动化测试
         setLifeCycleNum(state.lifeCycleNum - 1)
       },
       complete(result) {
         console.log('redirectTo complete', result.errMsg)
         // 自动化测试
         setLifeCycleNum(state.lifeCycleNum + 1)
       },
     })
   },
   switchTab() {
     uni.switchTab({
       url: '/pages/tabBar/template',
       success(result) {
         console.log('switchTab success', result.errMsg)
         // 自动化测试
         setLifeCycleNum(state.lifeCycleNum + 1)
       },
       fail(error) {
         console.log('switchTab fail', error.errMsg)
         // 自动化测试
         setLifeCycleNum(state.lifeCycleNum - 1)
       },
       complete(result) {
         console.log('switchTab complete', result.errMsg)
         // 自动化测试
         setLifeCycleNum(state.lifeCycleNum + 1)
       },
     })
   },
   // 自动化测试
   getLifeCycleNum(): number {
     return state.lifeCycleNum
   },
   // 自动化测试
   setLifeCycleNum(num: number) {
     setLifeCycleNum(num)
   },
 },
}
</script>

<style>
.direction-row {
 flex-direction: row;
}
.label {
 width: 190px;
}
</style>

# # General type

# # GeneralCallbackResult

name type optinal default description
errMsg string YES - 错误信息

# Bug & Tips

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