[Vuejs]-How to store data with VueJS?

1👍

I’m going to assume you’re working with an Object with no functions and just properties which you want to deep copy into a new object. If you’re certain you’re working with JSON-like data, you could use JSON.parse and JSON.stringify, which should be a speedy way to deep clone an object like that:

var device = JSON.parse(JSON.stringify(app.currentDevice));

This newly created device variable will be independent of the original variable and you can modify app.currentDevice without the other variable updating with it.

1👍

In your function that handles the push from currentDevice to devices, you need to CLONE the current device object, not push it as is, as that will simply create a reference from the currentDevices to the array. If you are using ES6, you can use Object.assign(), thus:

hander () {
  // copy this.current device props to new empty object
  const device = Object.assign({}, this.currentDevice)
  // push this new object
  devices.push(device)
  // do whatever you need to do to reset currentDevice ...
}

If you aren’t using ES6, you can use underscore or lodash equivalent methods, I think it’s _.extend() if I’m not mistaken

Leave a comment