I am currently working on a mini shop app using VueJS without a backend, focusing on adding items to the shopping cart only.
Within Component One (Nmdgrey.vue), I have boots along with an "Add to Cart" button.
<button @click="addToCart">Add to cart</button>
The function in the first component looks like this:
methods: {
addToCart() {
const boots = { name: 'adidas nmd' };
this.cart.push(boots);
}
}
Additionally, I have Component Two which displays the shopping cart.
<div v-for="item in cart">
{{item.name}}
</div>
In JavaScript:
import Nmdgrey from '@/components/Nmdgrey.vue';
export default {
name: 'Shoppingcart',
components:
Nmdgrey,
data() {
return {
cart: [
{ name: 'adidas adizero' },
]
}
},
};
How can I add boots from Component One to the list in Component Two?
Even though I used this.cart.push(boots);
in Component One, it didn't work as expected.
Here is what I want, but unfortunately, the button doesn't work: codesandbox