Can you post more of your child component? Specifically how you're setting selectedProduct and emitting it to the parent?
Prevent reactivity of data once pushed to an array.
Hi , I need some help from you guys.
I have VUE component which contains an array of products (ShoppingCart).
data() {
return {
shoppingCart: [],
};
},
I then have a child component which has an object (selected product)
data() {
return {
selectedProduct: [],
};
},
When the user selects a product, the selected product gets assigned to the selectedProduct object for the user to then customise, size, color etc. When the user clicks Add To Cartthe selectedProduct object get pushed to the Shopping Cart. ($emit) etc.
Problem is, I then want the user to be able to select a new product to customize. What is happening when the user selects a new product, and customizes it, it manipulates the data that has already been pushed to the shoppingCart[] array. So all products seem to have the same customizations.
How would I code it so that when the user selects a product a new 'instance' of selectedProduct is created? or that when the selectedProduct is pushed, it is no longer linked to the corresponding array item in the basket?
Thanks
Mitch
You have to "clone" the data, so you don't push a reference to the original variable into the array, but a new one, which isn't coupled to the select. If you're Using lodash, you could try
$emit('your-event', _.cloneDeep(your-var))
a possible way without lodash would be
$emit('your-event', JSON.parse(JSON.stringify(your-var)))
Please or to participate in this conversation.