magic bean
magic bean

Reputation: 797

How to use the component as a button in Vue.js

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.

Upvotes: 2

Views: 424

Answers (1)

Guille
Guille

Reputation: 714

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.

Upvotes: 1

Related Questions