Watch route changes in Vue.js with Typescript

luthien picture luthien · Aug 17, 2018 · Viewed 11.3k times · Source

I'd like to detect changes in my URL in a Vue.js application using TypeScript. In JavaScript this can be done as follows:

watch: {
  '$route': {
     handler: 'onUrlChange',
     immediate: true,
     deep: true
   }
},

onUrlChange(newUrl){
   // Some action
}

How could I do the same in TypeScript?

Answer

luthien picture luthien · Aug 17, 2018

I found the solution here: https://github.com/kaorun343/vue-property-decorator

It can be done like this:

   @Watch('$route', { immediate: true, deep: true })
   onUrlChange(newVal: any) {
      // Some action
    }