Vuelidate: Watch on $v throw an error

Created on 1 May 2017  路  4Comments  路  Source: vuelidate/vuelidate

When you watch $v, vue throws a Type error:Cannot read property '$refs' of undefined.
You can reproduce this at jsfiddle.

We can bypass this error with a custom hight order function on touch() or other stuff but it's sometime easy to manage that on a watcher.

Most helpful comment

This error came up because $v is a computed prop defined during beforeCreate handler.
When you declare your component watch try to get value of $v, but at this time this is not defined and you have this Type error.

What you can do it's to call $watch inside the created handler.

new Vue({
  ...

  created(){
    this.$watch('$v', () => {
      console.log('$v changed')
    })
  },

  validations: {
    ...
  }
})

All 4 comments

did you get something here?

This error came up because $v is a computed prop defined during beforeCreate handler.
When you declare your component watch try to get value of $v, but at this time this is not defined and you have this Type error.

What you can do it's to call $watch inside the created handler.

new Vue({
  ...

  created(){
    this.$watch('$v', () => {
      console.log('$v changed')
    })
  },

  validations: {
    ...
  }
})

@thibremy: Was pulling my hair out all afternoon because of this. Thanks for your solution! 馃榾

Hi, sorry for not comenting on that earlier. The original code should now just work as expected on v0.5.0. Fixed :)

Was this page helpful?
0 / 5 - 0 ratings

Related issues

daverogers picture daverogers  路  3Comments

mrcha90 picture mrcha90  路  3Comments

hootlex picture hootlex  路  3Comments

ecmel picture ecmel  路  3Comments

hackuun picture hackuun  路  4Comments