xiaokuaisong-xiaochengxu/uniapp04/pages/forums/forums.vue
2024-10-18 15:53:00 +08:00

43 lines
937 B
Vue

<template>
<!-- 测试页面 -->
<view class="cart-page">
<view v-for="(item, index) in menuItems" :key="index">
<view>{{ item.name }} - {{ item.price }}元</view>
<button @click="addToCart(item)">+</button>
</view>
<!-- 购物车列表 -->
<view v-if="cart.length > 0">
<view>购物车中的商品:</view>
<view v-for="(cartItem, cartIndex) in cart" :key="cartIndex">
{{ cartItem.name }} - {{ cartItem.price }}
<button @click="removeFromCart(cartIndex)">-</button>
</view>
</view>
</view>
</template>
<script>
export default {
data() {
return {
menuItems: [
{ name: '牛肉面', price: 18 },
{ name: '炒饭', price: 15 },
],
cart: []
};
},
methods: {
addToCart(item) {
this.cart.push({ ...item });
},
removeFromCart(index) {
this.cart.splice(index, 1);
}
}
};
</script>