Vue.js - don't bind an object when copying it to another data attribute

I have an object coming from my API and when loading a modality I need to "duplicate" the object to another.

It works:

this.servicesForm.services = this.team.services;

// New object                // API object

      

Now the problem is that I do NOT want the team.services object to bind and update when the servicesForm.services object is updated.

How to do it?

+3


source to share


2 answers


Found my answer quickly:



this.servicesForm.services = JSON.parse(JSON.stringify(this.team.services));

      

+3


source


ES6 solution is for using Object.assign:

this.servicesForm.services = Object.assign({}, this.team.services); 

      



Note that this is only a shallow copy, if you want a deep copy you will need to apply this method recursively.

Link: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/assign

0


source







All Articles