Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I avoid reactivity in vue

Tags:

laravel

vue.js

I'm assigning an object from property 'model' (which i get from blade attribute model in Laravel) to data attribute model. Later data attribute model changes since it's binded to form input field. But prop 'model' also changes. They are the same object. How can I avoid that behavior? I need property 'model' to stay as it was when mounted. I tried this in created() method and in data():

 Vue.set(this, 'model', this.transferredData.model);

That didn't help

like image 307
PttRulez Avatar asked Dec 06 '19 13:12

PttRulez


People also ask

How does reactivity work in Vue?

Vue's reactivity system works by deeply converting plain JavaScript objects into reactive proxies. The deep conversion can be unnecessary or sometimes unwanted when integrating with external state management systems (e.g. if an external solution also uses Proxies).

Are Vue methods reactive?

Vue comes with the reactive method, which is used to create a reactive state in JavaScript or Vue. Basically, the reactive method is just a function that creates a proxy and wraps it around provided data objects, ultimately turning it into a proxied object.

What is a reactive object in Vue?

The Vue JavaScript framework is "reactive," which means it can automatically refresh your data. Learn the ways you can take advantage of this in your apps. Modern web applications need to render data dynamically in various HTML elements.


1 Answers

You can use spread syntax to create a new object so it will mutate the newly created object only.

Vue.set(this, 'model', {...this.transferredData.model});
// or Vue.set(this, 'model', Object.assign({}, this.transferredData.model)); 

NOTE: If it is nested then you have to deconstruct recursively or you can use:

Vue.set(this, 'model',JSON.parse(JSON.stringify(this.transferredData.model)))
like image 98
Pranav C Balan Avatar answered Oct 23 '22 15:10

Pranav C Balan