watch监听路由变化详解

时间:2021-09-14 11:20:17 类型:vue
字号:    

  一、watch监听路由的方法

  通过watch监听,当路由发生变化的时候执行。

  方法一:

watch:{
  $router(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);
  }
  }


<