Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vue Watch not triggering

Trying to use vue watch methods but it doesn't seem to trigger for some objects even with deep:true.

In my component, I recieve an array as a prop that are the fields to create the following forms. I can build the forms and dynamicly bind them to an object called crudModelCreate and everything works fine (i see in vue dev tools and even submiting the form works according to plan)

But I have a problem trying to watch the changes in that dynamic object.

  <md-input v-for="(field, rowIndex) in fields" :key="field.id" v-model="crudModelCreate[field.name]" maxlength="250"></md-input>     ...     data() {       return {          state: 1, // This gets changed somewhere in the middle and changes fine          crudModelCreate: {},       }    },    ...    watch: {         'state': {             handler: function(val, oldVal) {                 this.$emit("changedState", this.state);                 // this works fine             },         },         'crudModelCreate': {             handler: function(val, oldVal) {                 console.log("beep1")                 this.$emit("updatedCreate", this.crudModelCreate);                 // This doesn't work             },             deep: true,             immediate: true         },     } 
like image 920
Sérgio Reis Avatar asked Oct 10 '17 12:10

Sérgio Reis


People also ask

How do you trigger a Vue watch?

To trigger the Vue watch method, you should assign a new city name to the value property of the ref object. Do not reset the city variable itself.

Can you watch a prop Vue?

To listen for props changes with Vue. js, we can add a watcher for the prop. to watch the value of the myProp prop by setting watch. myProp to an object with immediate set to true to watch the initial value of the myProp prop.

How do you use the watch function in Vue Cinema?

The Vue. js Watcher or watch property allows the developers to listen to the component data and run whenever they change a particular property. The watcher or watch property is a unique Vue. js feature that lets you keep an eye on one property of the component state and run a function when that property value changes.

How do I watch data changes on Vue instance?

A Watcher in Vue. js is a special feature that allows one to watch a component and perform specified actions when the value of the component changes. It is a more generic way to observe and react to data changes in the Vue instance. Watchers are the most useful when used to perform asynchronous operations.


1 Answers

From the docs

Due to the limitations of modern JavaScript (and the abandonment of Object.observe), Vue cannot detect property addition or deletion. Since Vue performs the getter/setter conversion process during instance initialization, a property must be present in the data object in order for Vue to convert it and make it reactive.

Please take a look to Reactivity in Depth https://vuejs.org/v2/guide/reactivity.html#Change-Detection-Caveats

like image 171
ricardoorellana Avatar answered Sep 30 '22 22:09

ricardoorellana