interceptor.uvue 3.5 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 9 10
    <navigator url="./page1">
      <button class="navigatorButton">navigator组件跳转到测试页面</button>
    </navigator>
    <button class="navigatorButton" @click="switchTab">switchTab API</button>
11
  </view>
Q
qiang 已提交
12 13 14
</template>

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

  const switchTabInterceptor = {
    invoke: function (options : SwitchTabOptions) {
      console.log('拦截 switchTab 接口传入参数为:', options)
      options.url = 'pages/tabBar/API'
    },
    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)
    }
  } as Interceptor

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