Files
xiaokuaisong-xiaochengxu/uniapp04/pages/forums/forums.vue
2025-08-18 14:20:34 +08:00

43 lines
937 B
Vue
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<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>