Vue 监听路由变化

Vue 监听路由

Vue 通过 watch监听路由变化

// 监听,当路由发生变化的时候执行
watch:{
  $route(to, from){
    console.log(to.path);
  }
},

或者

// 监听,当路由发生变化的时候执行
watch: {
  $route: {
    handler: function(newVal, oldVal){
      console.log(newVal);
    },
    // 深度观察监听
    deep: true
  }
},

或者

// 监听,当路由发生变化的时候执行
watch: {
  '$route':'getPath'
},
methods: {
  getPath(){
    console.log(this.$route.path);
  }
}

 

posted @ 2020-09-14 14:55  格鲁特baby  阅读(152)  评论(0编辑  收藏  举报