interceptor.uvue 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. <template>
  2. <view style="flex: 1">
  3. <button @click="addInterceptor">添加路由拦截器</button>
  4. <button @click="removeInterceptor">移除路由拦截器</button>
  5. <text>点击下方按钮{{ msg }}</text>
  6. <button @click="navigateTo">navigatorTo API跳转到测试页面</button>
  7. <!-- #ifndef MP -->
  8. <navigator url="./page1">
  9. <button class="navigatorButton">navigator组件跳转到测试页面</button>
  10. </navigator>
  11. <!-- #endif -->
  12. <button @click="addSwitchTabInterceptor">添加switchTab拦截器</button>
  13. <button @click="removeSwitchTabInterceptor">移除switchTab拦截器</button>
  14. <button @click="switchTab">switchTab API</button>
  15. </view>
  16. </template>
  17. <script>
  18. const navigateToInterceptor = {
  19. invoke: function (options : NavigateToOptions) {
  20. console.log('拦截 navigateTo 接口传入参数为:', options)
  21. const url = './page2'
  22. uni.showToast({
  23. title: `重定向到页面:${url}`
  24. })
  25. options.url = url
  26. },
  27. success: function (res : NavigateToSuccess) {
  28. console.log('拦截 navigateTo 接口 success 返回参数为:', res)
  29. },
  30. fail: function (err : NavigateToFail) {
  31. console.log('拦截 navigateTo 接口 fail 返回参数为:', err)
  32. },
  33. complete: function (res : NavigateToComplete) {
  34. console.log('拦截 navigateTo 接口 complete 返回参数为:', res)
  35. }
  36. } as AddInterceptorOptions
  37. const switchTabInterceptor = {
  38. invoke: function (options : SwitchTabOptions) {
  39. console.log('拦截 switchTab 接口传入参数为:', options)
  40. options.url = '/pages/tabBar/API'
  41. },
  42. success: function (res : SwitchTabSuccess) {
  43. console.log('拦截 switchTab 接口 success 返回参数为:', res)
  44. },
  45. fail: function (err : SwitchTabFail) {
  46. console.log('拦截 switchTab 接口 fail 返回参数为:', err)
  47. },
  48. complete: function (res : SwitchTabComplete) {
  49. console.log('拦截 switchTab 接口 complete 返回参数为:', res)
  50. }
  51. } as AddInterceptorOptions
  52. export default {
  53. data() {
  54. return {
  55. msg: "会跳转到测试页面1"
  56. }
  57. },
  58. beforeUnmount() {
  59. // 移除 navigateTo 所有拦截器
  60. uni.removeInterceptor('navigateTo')
  61. uni.removeInterceptor('switchTab')
  62. },
  63. methods: {
  64. addInterceptor() {
  65. uni.addInterceptor('navigateTo', navigateToInterceptor)
  66. uni.showToast({
  67. title: '页面跳转/切换tabbar已拦截'
  68. })
  69. this.msg = ",路由被劫持到测试页面2"
  70. },
  71. removeInterceptor() {
  72. uni.removeInterceptor('navigateTo', navigateToInterceptor)
  73. uni.showToast({
  74. title: '拦截器已移除'
  75. })
  76. this.msg = "会跳转到测试页面1"
  77. },
  78. addSwitchTabInterceptor() {
  79. uni.addInterceptor('switchTab', switchTabInterceptor)
  80. },
  81. removeSwitchTabInterceptor() {
  82. uni.removeInterceptor('switchTab', switchTabInterceptor)
  83. },
  84. navigateTo() {
  85. uni.navigateTo({
  86. url: './page1',
  87. success(res) {
  88. console.log('res:', res)
  89. },
  90. fail(err) {
  91. console.error('err:', err)
  92. },
  93. complete(res) {
  94. console.log('res:', res)
  95. }
  96. })
  97. },
  98. switchTab() {
  99. uni.switchTab({
  100. url: '/pages/tabBar/component',
  101. success(res) {
  102. console.log('res:', res)
  103. },
  104. fail(err) {
  105. console.error('err:', err)
  106. },
  107. complete(res) {
  108. console.log('res:', res)
  109. }
  110. })
  111. }
  112. }
  113. }
  114. </script>