interceptor.uvue 3.7 KB
Newer Older
Q
qiang 已提交
1
<template>
2
  <view style="flex: 1">
3 4
    <button @click="addInterceptor">添加路由拦截器</button>
    <button @click="removeInterceptor">移除路由拦截器</button>
5 6
    <text>点击下方按钮{{ msg }}</text>
    <button @click="navigateTo">navigatorTo API跳转到测试页面</button>
H
hdx 已提交
7 8
    <navigator url="./page1">
      <button class="navigatorButton">navigator组件跳转到测试页面</button>
9 10
    </navigator>
    <button @click="addSwitchTabInterceptor">添加switchTab拦截器</button>
11
    <button @click="removeSwitchTabInterceptor">移除switchTab拦截器</button>
H
hdx 已提交
12
    <button class="navigatorButton" @click="switchTab">switchTab API</button>
13
  </view>
Q
qiang 已提交
14 15 16
</template>

<script>
H
hdx 已提交
17
  const navigateToInterceptor = {
18
    invoke: function (options : NavigateToOptions) {
Q
qiang 已提交
19
      console.log('拦截 navigateTo 接口传入参数为:', options)
20 21 22 23 24 25
      const url = './page2'
      uni.showToast({
        title: `重定向到页面:${url}`
      })
      options.url = url
    },
26
    success: function (res : NavigateToSuccess) {
Q
qiang 已提交
27
      console.log('拦截 navigateTo 接口 success 返回参数为:', res)
28 29
    },
    fail: function (err : NavigateToFail) {
Q
qiang 已提交
30 31 32 33
      console.log('拦截 navigateTo 接口 fail 返回参数为:', err)
    },
    complete: function (res : NavigateToComplete) {
      console.log('拦截 navigateTo 接口 complete 返回参数为:', res)
Q
qiang 已提交
34
    }
35
  } as AddInterceptorOptions
H
hdx 已提交
36 37 38 39

  const switchTabInterceptor = {
    invoke: function (options : SwitchTabOptions) {
      console.log('拦截 switchTab 接口传入参数为:', options)
40
      options.url = '/pages/tabBar/API'
H
hdx 已提交
41 42 43 44 45 46 47 48 49 50
    },
    success: function (res : SwitchTabSuccess) {
      console.log('拦截 switchTab 接口 success 返回参数为:', res)
    },
    fail: function (err : SwitchTabFail) {
      console.log('拦截 switchTab 接口 fail 返回参数为:', err)
    },
    complete: function (res : SwitchTabComplete) {
      console.log('拦截 switchTab 接口 complete 返回参数为:', res)
    }
51
  } as AddInterceptorOptions
H
hdx 已提交
52

53 54 55 56 57 58 59 60 61
  export default {
    data() {
      return {
        msg: "会跳转到测试页面1"
      }
    },
    beforeUnmount() {
      // 移除 navigateTo 所有拦截器
      uni.removeInterceptor('navigateTo')
H
hdx 已提交
62
      uni.removeInterceptor('switchTab')
63 64 65
    },
    methods: {
      addInterceptor() {
H
hdx 已提交
66
        uni.addInterceptor('navigateTo', navigateToInterceptor)
67
        uni.showToast({
H
hdx 已提交
68
          title: '页面跳转/切换tabbar已拦截'
69 70 71 72
        })
        this.msg = ",路由被劫持到测试页面2"
      },
      removeInterceptor() {
H
hdx 已提交
73
        uni.removeInterceptor('navigateTo', navigateToInterceptor)
74 75 76 77
        uni.showToast({
          title: '拦截器已移除'
        })
        this.msg = "会跳转到测试页面1"
78 79 80 81 82 83
      },
      addSwitchTabInterceptor() {
        uni.addInterceptor('switchTab', switchTabInterceptor)
      },
      removeSwitchTabInterceptor() {
        uni.removeInterceptor('switchTab', switchTabInterceptor)
84 85 86 87 88 89 90 91 92
      },
      navigateTo() {
        uni.navigateTo({
          url: './page1',
          success(res) {
            console.log('res:', res)
          },
          fail(err) {
            console.error('err:', err)
Q
qiang 已提交
93 94 95
          },
          complete(res) {
            console.log('res:', res)
96 97
          }
        })
H
hdx 已提交
98 99 100
      },
      switchTab() {
        uni.switchTab({
101
          url: '/pages/tabBar/component',
H
hdx 已提交
102 103 104 105 106 107 108 109 110 111
          success(res) {
            console.log('res:', res)
          },
          fail(err) {
            console.error('err:', err)
          },
          complete(res) {
            console.log('res:', res)
          }
        })
112 113 114
      }
    }
  }
115
</script>