# uni.addInterceptor(name, interceptor)

添加拦截器

# # Parameters

name type required default description
name string YES - 需要拦截的 API 名称
interceptor Interceptor YES - 拦截器

# # Interceptor Values

name type optinal default description
invoke (...args?: any) => any NO - 拦截前触发
returnValue (...args?: any) => any NO - 方法调用后触发,处理返回值
success (...args?: any) => any NO - 成功回调拦截
fail (...args?: any) => any NO - 失败回调拦截
complete (...args?: any) => any NO - 完成回调拦截

# # addInterceptor Compatibility

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

# # See also

Related Bug

# uni.removeInterceptor(name, interceptor?)

删除拦截器

# # Parameters

name type required default description
name string YES - 需要删除拦截器的 API 名称
interceptor Interceptor NO - 拦截器

# # Interceptor Values

name type optinal default description
invoke (...args?: any) => any NO - 拦截前触发
returnValue (...args?: any) => any NO - 方法调用后触发,处理返回值
success (...args?: any) => any NO - 成功回调拦截
fail (...args?: any) => any NO - 失败回调拦截
complete (...args?: any) => any NO - 完成回调拦截

# # removeInterceptor Compatibility

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

# # See also

Related Bug

# # Example

hello uni-app x

<template>
 <view>
   <button @click="addInterceptor">添加路由拦截器</button>
   <button @click="removeInterceptor">移除路由拦截器</button>
   <text>点击下方按钮{{msg}}</text>
   <button @click="navigateTo">跳转到测试页面</button>
 </view>
</template>

<script>
 const interceptor = {
   invoke: function (options : NavigateToOptions) {
     console.log('拦截 navigateTo 接口传入参数为:', options)
     const url = './page2'
     uni.showToast({
       title: `重定向到页面:${url}`
     })
     options.url = url
   },
   success: function (res : NavigateBackSuccess) {
     console.log('拦截 navigateTo 接口 success 返回参数为:', res)
   },
   fail: function (err : NavigateToFail) {
     console.log('拦截 navigateTo 接口 fail 返回参数为:', err)
   },
   complete: function (res : NavigateToComplete) {
     console.log('拦截 navigateTo 接口 complete 返回参数为:', res)
   }
 } as Interceptor
 export default {
   data() {
     return {
       msg: "会跳转到测试页面1"
     }
   },
   beforeUnmount() {
     // 移除 navigateTo 所有拦截器
     uni.removeInterceptor('navigateTo')
   },
   methods: {
     addInterceptor() {
       uni.addInterceptor('navigateTo', interceptor)
       uni.showToast({
         title: '页面跳转已拦截'
       })
       this.msg = ",路由被劫持到测试页面2"
     },
     removeInterceptor() {
       uni.removeInterceptor('navigateTo', interceptor)
       uni.showToast({
         title: '拦截器已移除'
       })
       this.msg = "会跳转到测试页面1"
     },
     navigateTo() {
       uni.navigateTo({
         url: './page1',
         success(res) {
           console.log('res:', res)
         },
         fail(err) {
           console.error('err:', err)
         },
         complete(res) {
           console.log('res:', res)
         }
       })
     }
   }
 }
</script>

<style>

</style>

# # General type

# # GeneralCallbackResult

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

# Tips

  • 目前仅以下接口支持拦截器:navigateTo、redirectTo、reLaunch、switchTab、navigateBack、loadFontFace、pageScrollTo、startPullDownRefresh、setNavigationBarColor、setNavigationBarTitle、setTabBarBadge、removeTabBarBadge、setTabBarItem、setTabBarStyle、hideTabBar、showTabBar、showTabBarRedDot、hideTabBarRedDot
  • 如需拦截request,可在插件市场搜索拦截器插件