Watch route object on vue js

Komang Suryadana picture Komang Suryadana · Jun 22, 2018 · Viewed 32.4k times · Source

How to watch route object on vue js?. 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 sanbox 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

Answer

ittus picture ittus · Jun 22, 2018

Did you try deep option?

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