interceptor.uvue 2.0 KB
Newer Older
Q
qiang 已提交
1 2
<template>
    <view>
W
wanganxp 已提交
3 4 5
        <button @click="addInterceptor">添加路由拦截器</button>
        <button @click="removeInterceptor">移除路由拦截器</button>
        <text>点击下方按钮{{msg}}</text>
Q
qiang 已提交
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
        <button @click="navigateTo">跳转到测试页面</button>
    </view>
</template>

<script>
    const interceptor = {
        invoke: function (options : NavigateToOptions) {
            console.log('interceptor.invoke:', options)
            const url = './page2'
            uni.showToast({
                title: `重定向到页面:${url}`
            })
            options.url = url
        },
        success: function (res : NavigateBackSuccess) {
            console.log('interceptor.success:', res)
        },
        fail: function (err : NavigateToFail) {
            console.log('interceptor.fail:', err)
        }
    } as Interceptor
    export default {
        data() {
            return {
W
wanganxp 已提交
30
              msg:"会跳转到测试页面1"
Q
qiang 已提交
31 32 33 34 35 36
            }
        },
        methods: {
            addInterceptor() {
                uni.addInterceptor('navigateTo', interceptor)
                uni.showToast({
W
wanganxp 已提交
37 38 39
                    title: '页面跳转已拦截'
                })
                this.msg = ",路由被劫持到测试页面2"
Q
qiang 已提交
40 41 42 43 44
            },
            removeInterceptor() {
                uni.removeInterceptor('navigateTo', interceptor)
                uni.showToast({
                    title: '拦截器已移除'
W
wanganxp 已提交
45 46
                })
                this.msg = "会跳转到测试页面1"
Q
qiang 已提交
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
            },
            navigateTo() {
                uni.navigateTo({
                    url: './page1',
                    success(res) {
                        console.log('res:', res)
                    },
                    fail(err) {
                        console.error('err:', err)
                    }
                })
            }
        }
    }
</script>

<style>

W
wanganxp 已提交
65
</style>