vue watch监听路由变化

vue watch监听路由变化

// 监听 this.$route.path 
// watch监听非DOM元素的改变
watch:{
 '$route.path':function(to,from){
    console.log(to);
  }
},

// 监听,当路由发生变化的时候执行
watch:{
  $route(to,from){
    console.log(to.path);
  }
},
或者
// 监听,当路由发生变化的时候执行
watch: {
  $route: {
    handler: function(val, oldVal){
      console.log(val);
    },
    // 深度观察监听
    deep: true
  }
},
或者
// 监听,当路由发生变化的时候执行
watch: {
  '$route':'getPath'
},
methods: {
  getPath(){
    console.log(this.$route.path);
  }
}

原文地址:https://www.cnblogs.com/niuben/p/14890233.html