1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
| <script setup lang="ts"> import { computed, reactive, ref } from 'vue' type Shop = { name: string, num: number, price: number } let $total = ref<number>(0) const data = reactive<Shop[]>([ { name: "袜子", num: 1, price: 100 }, { name: "裤子", num: 1, price: 200 }, { name: "衣服", num: 1, price: 300 }, { name: "毛巾", num: 1, price: 400 } ]) const AddAnbSub = (item: Shop, type: boolean = false): void => { if (item.num > 1 && !type) { item.num-- } if (item.num <= 99 && type) { item.num++ } } const del = (index: number) => { data.splice(index, 1) } $total = computed<number>(() => { return data.reduce((prev, next) => { return prev + (next.num * next.price) }, 0) }) </script> <style> </style>
|