Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to send & receive object as a props in Vue.js

I have a Product object and I am taking it from API call. So in parent component I want to send this object into the child object which is SoldTickets component.

So here is my parent component:

<template>
    <section id="cart-page" class="row">
        <div v-for="item in cart.attributes.items" :key="item.id">
            <div class="col-lg-8 col-md-12 col-sm-12 col-xs-12">
                <div class="title">WARENKORB</div>
                <SoldTickets :item = item />
            </div>
        </div>
    </section>
</template>

And my child:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

<template>
    <div id="sold-tickets">
        <div class="card">
            <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>
                    <DeleteButton @click.prevent="removeProductFromCart(item.id)" />
                </div>
            </div>
        </div>
    </div>
</template>
<script>
export default {
    props: {
        data: {
            item: Object,
        }
    },
}
</script>

So I am quite new in Vue so I am not really confident with props. COuld you please help me with this.

>Solution :

You mostly have it right, but there are a couple of errors:

Your props aren’t declared correctly in the child component, you shouldn’t have the "data" in there. All props you want to declare go directly under the "props" key of the component declaration:

export default {
    props: {
        item: Object,
    },
}

You’re also missing quotes around the attribute value in the parent component, so that’s invalid HTML:

<SoldTickets :item = item />

should be

<SoldTickets :item = "item" />
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading