Answer the question
In order to leave comments, you need to log in
How to optimize the management of items in a list in Vuex?
I receive categories of the form from the server
{
id: 1,
title: "category",
products: [
id: 1,
title: "product",
subproducts: [
id: 1,
title: "subproduct",
]
]
}
ADD_PRODUCT(state, { categoryName, price, title, subproducts = [] }) {
let categories = state.categories
if (!categories) return
let category = _.find(categories, { title: categoryName })
if (!category) return
category.products.push({ price, title, subproducts })
},
REMOVE_SUBPRODUCT(state, { categoryId, productId, id }) {
let categories = [...state.categories]
let category = _.find(categories, { id: categoryId })
if (!category) return
let product = _.find(category.products, { id: productId })
if (!product) return
_.remove(product.subproducts, { id })
state.categories = categories
},
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question