2

I have a view of Cart and it's child component is SoldTickets and in SoldTicket component I have delete button component.

enter image description here

So I am showing cart items into my SoldTickets component as you see the code below:

<template>
    <div id="sold-tickets">
        <div class="card" v-for="item in cart.attributes.items" :key="item.id">
            <div class="sold-tickets-actions">
                <div class="sold-tickets-inner">
                    <img class="sold-tickets-image" :src="image" alt="Sold Ticket"/>
                </div>
            </div>
            <div class="sold-tickets-actions properties">
                <div class="sold-tickets-inner">
                    <div class="ticket-details">
                        <div class="ticket-prop">
                            <div class="ticket-name">{{ item.product_name }}</div>
                            <div class="ticket-type">{{ item.variation_name }}</div>
                        </div>
                        <div class="ticket-prop">
                            <div class="price-prop">
                                <div class="count">{{ item.amount }}</div>
                                <div>x</div>
                                <div class="price">€{{ item.total_price_incl_vat }}</div>
                            </div>
                            <div class="article-prop">
                                <div class="article-number">{{ item.updated_at }}</div>
                                <div>-</div>
                                <div class="ticket-category">{{ item.product_short_description }}</div>
                            </div>
                        </div>
                    </div>
                    <DeleteButton @click.prevent="removeProductFromCart(item.id)" />
                </div>
            </div>
        </div>
    </div>
</template>
<script>
import image from "../../../../img/Hallenbad.jpg";
import DeleteButton from "./DeleteButton";
import cartHelper from "../helpers/cartHelper";

export default {
    components: {DeleteButton},
    data() {
        return {
            image: image,
        };
    },
    computed: {
        cart() {
            return this.$store.state.cart;
        },
    },
    methods: {
        removeProductFromCart(id) {
            cartHelper.removeFromCart(id, (response) => {
                this.$store.dispatch('removeProductFromCart', {
                    cart: response.data,
                })
            });
        },
    },
};
</script>

So basically I am storing the cart in Vuex store and I want to delete the item when I click the delete component (That means I want to use my delete component like a button). But I am not able to do it and I dont get any error.

1
  • It's not clear what problem you are having. Is the item not being removed from Vuex? Commented Nov 12, 2021 at 14:05

1 Answer 1

1

On DeleteButton you need to emit the click function, for example:

<template>
  <button @click="$emit('click')">
    Delete
  </button>
</template>

And on SoldTickets

<DeleteButton @click="removeProductFromCart(item.id)" />

@click es the name of the emitted function from the button. By doing this you are able to catch the event and assign a function to do something!

Note: I think .prevent is not needed because you are not in a form.
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.