[Vuejs]-Two objects have the same value

0👍

You are mutating a reference value, it doesn’t matter which one you change – they both get updated as they share the same address in memory heap.

So Use ES6 spread to make clone when you are trying to have copy of any reference value.

this.user = { ...this.tuser }

or

this.tuser = { ...this.user }

References : spread_es6

0👍

It is most likely because you reference one object to another.
As objects are reference not primitive values.
What it means is:
if you make obj1 = obj2, then actually they both point to the same object, if you change one, another change also.
You need to use something like Object.assign()
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/assign

0👍

methods:{
  edit(){
  this.tuser = this.user
  },

I think you just need to pass a cloned copy of this.user, you can achieve that using lodash/underscore.js methods like clone or cloneDeep. If you don’t copy your object they are referencing the same object

Leave a comment