提交 480fd1f5 编写于 作者: Q qiang

fix: 更新拦截器示例,页面销毁时移除拦截器

上级 39c7e128
<template> <template>
<view> <view>
<button @click="addInterceptor">添加拦截器</button> <button @click="addInterceptor">添加拦截器</button>
<button @click="removeInterceptor">移除拦截器</button> <button @click="removeInterceptor">移除拦截器</button>
<button @click="navigateTo">跳转到测试页面</button> <button @click="navigateTo">跳转到测试页面</button>
</view> </view>
</template> </template>
<script> <script>
const interceptor = { const interceptor = {
invoke: function (options : NavigateToOptions) { invoke: function (options : NavigateToOptions) {
console.log('interceptor.invoke:', options) console.log('interceptor.invoke:', options)
const url = './page2' const url = './page2'
uni.showToast({ uni.showToast({
title: `重定向到页面:${url}` title: `重定向到页面:${url}`
}) })
options.url = url options.url = url
}, },
success: function (res : NavigateBackSuccess) { success: function (res : NavigateBackSuccess) {
console.log('interceptor.success:', res) console.log('interceptor.success:', res)
}, },
fail: function (err : NavigateToFail) { fail: function (err : NavigateToFail) {
console.log('interceptor.fail:', err) console.log('interceptor.fail:', err)
} }
} as Interceptor } as Interceptor
export default { export default {
data() { data() {
return { return {
} }
}, },
methods: { beforeUnmount() {
addInterceptor() { // 移除 navigateTo 所有拦截器
uni.addInterceptor('navigateTo', interceptor) uni.removeInterceptor('navigateTo')
uni.showToast({ },
title: '拦截器已添加' methods: {
}) addInterceptor() {
}, uni.addInterceptor('navigateTo', interceptor)
removeInterceptor() { uni.showToast({
uni.removeInterceptor('navigateTo', interceptor) title: '拦截器已添加'
uni.showToast({ })
title: '拦截器已移除' },
}) removeInterceptor() {
}, uni.removeInterceptor('navigateTo', interceptor)
navigateTo() { uni.showToast({
uni.navigateTo({ title: '拦截器已移除'
url: './page1', })
success(res) { },
console.log('res:', res) navigateTo() {
}, uni.navigateTo({
fail(err) { url: './page1',
console.error('err:', err) success(res) {
} console.log('res:', res)
}) },
} fail(err) {
} console.error('err:', err)
}
})
}
} }
}
</script> </script>
<style> <style>
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册