0

I have object with longitude and latitude?

data(){
  return{
  center2:{lat:0,lng:0}
}

}

I am trying to watch it like this but not working

 watch:{
    "center2.lat":function (newValue, oldValue) {
     
      this.center2.lat = newValue;
    }
    , "center2.lng":function (newValue, oldValue) {
     
      this.center2.lng = newValue;
    }
    },

How I could do it?

Swift Solutions
  • 179
  • 2
  • 12
  • 1
    Does this answer your question? [Vue.js - How to properly watch for nested data](https://stackoverflow.com/questions/42133894/vue-js-how-to-properly-watch-for-nested-data) – Reyno Nov 17 '21 at 09:11
  • https://v3.vuejs.org/guide/reactivity-computed-watchers.html#watching-reactive-objects – abhishekkannojia Nov 17 '21 at 09:16
  • This is weird code. You watch and mutate it. It’s like recursion for mutating state.. and keep mutating forever once the value changes – owenizedd Nov 17 '21 at 09:21

1 Answers1

0

Please consider this code example.

  1. Vue watch child property

    watch:{ 'item.someOtherProp'(newVal){ //to work with changes in "myArray" }, 'item.prop'(newVal){ //to work with changes in prop } }

  2. Vue watch object member

    watch: { item: { handler(val){ // do stuff }, deep: true } }

Also please consider this reference.https://vuejs.org/v2/api/#watch. You can deeply watch objects for changes.

GruDev325
  • 3
  • 2
  • 1
    Instead of copying code from a duplicate question its better to flag the current question as duplicate ([if you have enough reputation](https://stackoverflow.com/help/duplicates)). – Reyno Nov 17 '21 at 09:26