vue.jswatch

Watch route object in VueJS 2


How to watch route object on VueJS 2?. This is my code

watch: { 
  '$route.params.search': function(search) {
    console.log(search)
  }
}

It doesn't work.

I try use with deep still not working on here

Look on code sandbox you can watch route object on main.js.

You should not watch for the route object inside any other components. Because components get destroyed when router link changes. You should do it in the main.js file, according to your directory structure

Thanks @santanu and @ittus


Solution

  • Did you try deep option?

    watch: { 
      '$route.params.search': {
        handler: function(search) {
          console.log(search)
        },
        deep: true,
        immediate: true
      }
    }