상품추가하는 부분때문에 개고생함
filter() 함수가 작동되지 않고
splice(index, 1)은 되는데 filter()안돼서 개고생함
store.js
addItem(state, item) {
const resultItems = state.items.filter((cartItem) => {
//cartItem.id = item.id; 이렇게 해서 개고생
cartItem.id === item.id;
});
if (resultItems.length === 0) {
state.items.push({
...item,
qty: 1,
});
} else {
resultItems[0].qty++;
}
},
CartList.vue
<template>
<div>
<!-- Cart -->
<section class="cart bgwhite p-t-70 p-b-100">
<template v-for="(item, index) in items">
<tr :key="index" class="table-row">
<td class="column-1">
<div
class="cart-img-product b-rad-4 o-f-hidden"
@click="delItem(item.id)"
>
{{ item.id }}
<img :src="item.image" alt="IMG-PRODUCT" />
</div>
</td>
</tr>
</template>
</section>
</div>
</template>
<script>
import { mapState, mapGetters } from "vuex";
export default {
computed: {
...mapState(["items"]),
...mapGetters(["totalPrice"]),
},
methods: {
delItem(id) {
this.$store.dispatch("delItem", id);
},
},
};
</script>
<style></style>