Skip to content
Advertisement

Pass selected index of object that resides outside of a v-for loop in Vue 3

I’m creating a simple shopping cart in Vue 3 for learning purposes.

So far, I’ve set up everything from the products object to the addToCart() functionality, and everything works within a v-for loop.

The problem is I need to display the product’s title within an alert that resides outside the v-for loop and I have no idea how to do that in Vue.

I tried emit and provide but it’s not working. I can send the entire object to the child component Alert.vue via provide but that’s not helpful as I only need to get the current index of the selected product to be able to fetch its title.

You can check a demo here https://codesandbox.io/s/vue-cart-54ioqt?file=/src/App.vue

Try adding a product to the cart twice and check the Alert. At the moment it’s displaying the entire cart object but I only need to fetch the title of the product so that the alert would say You have already added {product.title} to your cart

App.vue

JavaScript

Alert.vue (child component)

JavaScript

Advertisement

Answer

You can show your prop product in Cart component:

JavaScript

In app add item to data function:

JavaScript

in method add title to that data property:

JavaScript

in template bind your property to item:

JavaScript

your demo

Advertisement