kidi
132
3
Vue

Pass array from child to parent

Posted 1 month ago by kidi

Hi!

I have two component.

The parent is: Worksheet The child is: ProductCart

In the ProductCart there is an array where can I put the items into the cart. If I submit the form in the parent, how can I get the products from the cart into the parent?

Currently I solved this in this way, but I truly believe that's not correct.

Parent:

    created() {
        Event.$on('reciveProductsFromProductCart', (product) => {
            this.productsCart = [];
            product.forEach((resource, index) => {
                Vue.set(this.productsCart, index, resource)
            });
        });
    },


    methods: {
        getProductsFromServiceProductCart() {
            Event.$emit('getProductsFromProductCart');
        },
    }

Child:

    created() {
        Event.$on('getProductsFromProductCart', () =>
            Event.$emit('receiveProductsFromProductCart', this.productsCart));
    }

Please sign in or create an account to participate in this conversation.