Recently, I've started using Vue and encountered a problem. I'm trying to access data stored inside an Object within one of my components. To practice, I decided to create a cart system with hardcoded data for a few games in the app. Below is the code snippet for the Object:
products: [
{
id: 1,
name: 'Call of Duty: Modern Warfare',
price: 'Rs. 5,500',
shortDesc: 'Call of Duty: MW returns. This game is a complete revamp of the old COD:MW',
description: 'Call of Duty: MW returns. This game is a complete revamp of the old COD:MW',
inStock: 'yes',
availableQuantity: '100',
img: 'src/assets/img/codmw.jpg',
},
{
id: 2,
name: 'PlayerUnknowns Battlegrounds',
price: 'Rs. 600',
shortDesc: 'Call of Duty: MW returns. This game is a complete revamp of the old COD:MW',
description: 'Call of Duty: MW returns. This game is a complete revamp of the old COD:MW',
inStock: 'yes',
availableQuantity: '500',
img: 'src/assets/img/pubg.jpg',
},
{
id: 3,
name: 'GTA VI',
price: 'Rs. 10,000',
shortDesc: 'Call of Duty: MW returns. This game is a complete revamp of the old COD:MW',
description: 'Call of Duty: MW returns. This game is a complete revamp of the old COD:MW',
inStock: 'yes',
availableQuantity: '5',
img: 'src/assets/img/gta.jpg',
}
]
The above data is located in the Shop.vue component. Whenever a user clicks on the "View Product" button, a separate route for that specific product is called. Now, I need to fetch the data of that particular game in the ViewProduct.vue component. For this purpose, I added a button in Shop.vue like the one below:
<router-link
class="btn btn-primary"
:to="/view/ + game.id"
v-b-tooltip.hover title="View Product details">
View Product
</router-link>
This button opens up a new route with the path "view/ID", where ID is dynamic. In the view route, I access this dynamic ID using:
this.$route.params.id
Now, my challenge is how to access the data of the product with an ID that matches the dynamic ID mentioned above in the ViewProduct.vue component since the Product Object is in the Shop.vue component?
If you have any suggestions or solutions, please help me out. Thank you!