vue监听路由方式

一、 通过 watch 进行监听

// 方式1、监听路由 $route 变化  一般监听
export default{
    watch: {
        $route(to, from){
            console.log('路由变化了')
            console.log('当前页面路由:' + to.path);
            console.log('上一个路由:' + from);
        },
    }
}
 
// 方式2、监听路由 $route 变化, 使用handler函数 深度监听路由
export default{
    watch: {
        '$route': { // $route可以用引号,也可以不用引号  监听的对象
            handler(to, from){
            console.log('路由变化了')
            console.log('当前页面路由:' + to);
            console.log('上一个路由:' + from);
            },
            deep: true, // 深度观察监听 设置为 true
            immediate: true, // 第一次初始化渲染就可以监听到
        }
    }
}
 
// 方式3、监听路由 $route 变化,触发methods里的方法
export default{
    watch: {
        '$route': 'initData'
    },
    methods: {
        initData(){
            console.log('路由变化了')
        }
    }
}
 
// 方式4、监听路由的 path 变化
export default{
    watch: {
        '$route.path'(toPath, fromPath){
            console.log('路由变化了')
            console.log('当前页面路由地址:' + toPath)
            console.log('上一个路由地址:' + fromPath)
         },
    }
}
 
// 方式5、监听路由的 path 变化, 使用handler函数
export default{
    watch: {
        '$route.path': {
            handler(toPath, fromPath){
                console.log('路由变化了')
                console.log('当前页面路由地址:' + toPath)
                console.log('上一个路由地址:' + fromPath)
            },
            deep: true, // 深度监听
            immediate: true, // 第一次初始化渲染就可以监听到
        }
    }
}
 
// 方式6、监听路由的 path 变化,触发methods里的方法
export default{
    watch: {
        '$route.path': 'initData'
    },
    methods: {
        initData(){
            console.log('路由变化了')
        }
    }
}

二、通过钩子函数beforeRouteEnter、beforeRouteUpdate、beforeRouteLeave进行监听


export default{
    beforeRouteEnter(to, from, next){
        // 渲染该组件前调用这个钩子,因此组件还未被创建,不能获取this
        console.log(this) // 结果为:undefined
        console.log('beforeRouteEnter')
        next()
    },
    beforeRouteUpdate(to, from, next){
        //这个组件是被复用的时候调用,比如带有动态参数的路由跳转:/add/11 跳转到 /detail/12
        console.log(this) // 可以访问this
        console.log('beforeRouteUpdate')
        next()
    },
    beforeRouteLeave(to, from, next){
        // 导航离开当前路由的时候被调用,this可以被访问到
        console.log(this) // 可以访问this
        console.log('beforeRouteLeave')
        next()
    },
}

三、全局路由监听 this.$router.beforeEach


// 方式1、在App.vue的create中进行全局路由监听
export default  {
    name:  'App',
    created() {
        this.$router.beforeEach((to, from, next) => {
            console.log(to);
            console.log(from);
            next()
        })
    }
}
 
// 方式2、在路由文件(/router/index.js)中进行全局路由监听
import Vue from 'vue'
import Router from 'vue-router'
Vue.use(Router)
 
let routes = [
     {
       path: '/login',
       component: resolve => require(['@/views/login'], resolve),
     },
]
 
let router = new Router({
    mode: 'history', // 去掉 url 中的 #
    scrollBehavior: () => ({ y: 0 }),
    base: process.env.VUE_APP_BASE_DOMAIN,
    routes,
})
 
router.beforeEach((to, from, next) => {
    console.log(to);
    console.log(from);
    next()
})
 
export {
    routes
    router
}

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值