// stores/cart.js import { defineStore } from 'pinia'; import { ref, computed } from 'vue'; export const useCartStore = defineStore('cart', () => { const items = ref([]); const totalItems = computed(() => items.value.reduce((sum, item) => sum + item.quantity, 0) ); function addToCart(product) { const existing = items.value.find((i) => i.id === product.id); if (existing) { existing.quantity++; } else { items.value.push({ ...product, quantity: 1 }); } } return { items, totalItems, addToCart }; }); Add {{ p.name }} Total items: {{ totalItems }} {{ item.name }} x{{ item.quantity }}
Total items: {{ totalItems }}