监听路由变化

1.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

}

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容