P
P
picka2020-12-04 22:43:24
JavaScript
picka, 2020-12-04 22:43:24

How to add a product to the cart in VUEX NUXT.JS, while adding a unique article to it?

PROBLEM: When adding a product to the cart, through mutation, I add the value *** to the product array in the "code" key - the generated article consisting of numbers. The article carries the model of the product and its color and size. That is, when choosing the same product but with a different color or size, the article changes. This is how it all happens, the article is generated when you click "ADD TO CART" and, together with the product itself, goes to the VUEX mutation, where it adds "article" to "code": "article", but if you stay on the page, and for example, twist the parameters of the same product further (well, for example, I want a sweatshirt in two sizes), then the article will change for the first one.

Here is the mutation code:

ADD_TO_CART(state, {post1, genCode}) {
        post1.code = genCode
        state.cart.push(post1)
},


Here is the code on the product page that simply mutates the product itself and the finished article
this.$store.commit('cart/ADD_TO_CART', {post1, genCode})


I tried to make a copy of the product, and send it, but it's still the same.

I am new to VUEX.
Help brothers.

____________________________________
I noticed that the wrong element of the goods that I send is mutating. And not mediocre its original in state. Tell me how to fix it.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
Nicholas, 2020-12-15
@healqq

Making a copy should be enough, otherwise you're putting multiple references to the same object in your array of items in the cart.

this.$store.commit('cart/ADD_TO_CART', {post1: { ...post1 }, genCode})

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question