VUE监听路由变化的方式

594 阅读1分钟

1、监听路由的来去

watch:{
    $route(to,from){
      console.log(from.path);//从哪来
      console.log(to.path);//到哪去
    }
}

2、监听路由的新老变化

watch:{
    $route:{
    handler(newVal,oldVal){
     console.log(newVal);//新信息
     console.log(oldVal);//老信息
    },
    deep: true
}

3、路由变化触发方法

watch:{
    '$route''func'
}
methods:{
func(){}
}

4、全局监听路由

在app.vue或者路由配置问价route.config.js里面

this.$router.beforeEach((to, from, next) => { console.log(to); next(); });