选中
图片
单价
个数
小计
操作
{{ item.price }}
{{ item.num }} - CSDN文库",
"datePublished": "2025-05-13",
"keywords": "
购物车
选中
图片
单价
个数
小计
操作
{{ item.price }}
{{ item.num }}",
"description": "文章浏览阅读28次。### Vue.js 购物车功能实现详解以下是基于 Vue.js 的购物车功能实现的代码解析以及改进建议:#### 1. 使用 `v-for` 渲染商品列表通过 `v-for` 指令可以动态渲染商品列表。每个商品对象通常包含名称、价格和数量等字段。```html
"
}
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>index.html</title> </head> <body>
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta http-equiv="X-UA-Compatible" content="IE=edge" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <link rel="stylesheet" href="./css/inputnumber.css" /> <link rel="stylesheet" href="./css/index.css" /> <script src="https://unpkg.com/vue@3"></script> <title>购物车</title> </head> <body> <div class="app-container" id="app"> <!-- 顶部banner --> <div class="banner-box"><img src="./img/fruit.jpg" alt="Fruit"></div> <!-- 面包屑 --> <div class="breadcrumb"> <span>🏠</span> / <span>购物车</span> </div> <!-- 购物车主体 --> <!-- v-if v-else 条件渲染,如果没有数据则显示空车 --> <div v-if="fruitList.length > 0" class="main"> <div class="table"> <!-- 头部 --> <div class="thead"> <div class="tr"> <div class="th">选中</div> <div class="th th-pic">图片</div> <div class="th">单价</div> <div class="th num-th">个数</div> <div class="th">小计</div> <div class="th">操作</div> </div> </div> <!-- 身体 --> <!-- v-for 循环遍历渲染数组 --> <div v-for="(item, index) in fruitList" :key="item.id" class="tbody"> <!-- v-bind 绑定 class 设置条件--> <div class="tr" :class="{ active: item.isChecked }"> <!-- v-model 绑定属性 --> <div class="td"><input type="checkbox" v-model="item.isChecked" /></div> <!-- v-bind 绑定 src --> <div class="td"><img :src="item.icon" alt="" /></div> <div class="td">{{ item.price }}</div> <div class="td"> <div class="my-input-number"> <!-- @click 点击事件实现数量加减 --> <!-- :disabled 是一个动态属性绑定,它允许你根据表达式的结果动态地设置 HTML 元素的 disabled 属性 --> <button :disabled="item.num <= 1" class="decrease" @click="item.num--"> - </button> <span class="my-input__inner">{{ item.num }}</span>
时间: 2025-05-13 12:56:13 浏览: 28
### Vue.js 购物车功能实现详解
以下是基于 Vue.js 的购物车功能实现的代码解析以及改进建议:
#### 1. 使用 `v-for` 渲染商品列表
通过 `v-for` 指令可以动态渲染商品列表。每个商品对象通常包含名称、价格和数量等字段。
```html
<div id="cart-app">
<ul>
<li v-for="(item, index) in cartItems" :key="index">
{{ item.name }} - ${{ item.price }}
<button @click="removeItem(index)">Remove</button>
Quantity:
<input type="number" v-model.number="item.quantity" min="0"/>
</li>
</ul>
<p>Total Price: ${{ calculateTotal() }}</p>
<form @submit.prevent="addItem">
<label>Name:</label><input v-model="newItem.name" />
<label>Price:</label><input v-model.number="newItem.price" />
<label>Quantity:</label><input v-model.number="newItem.quantity" />
<button type="submit">Add Item</button>
</form>
</div>
```
上述代码中,`v-for` 遍历了 `cartItems` 数组并显示每件商品的信息[^1]。同时,使用 `@click` 绑定移除按钮事件处理函数 `removeItem()` 来删除指定索引的商品。
#### 2. 双向数据绑定 `v-model`
`v-model` 实现了输入框与商品数量之间的双向绑定。当用户修改输入框中的数值时,对应的商品数量会实时更新到组件的数据模型中。
#### 3. 计算总金额方法
定义了一个计算总价的方法 `calculateTotal()`,它遍历所有商品并将单价乘以其数量求和。
```javascript
new Vue({
el: '#cart-app',
data: {
newItem: { name: '', price: 0, quantity: 1 },
cartItems: [
{ name: 'Apple', price: 1, quantity: 5 },
{ name: 'Banana', price: 0.5, quantity: 10 }
]
},
methods: {
addItem() {
this.cartItems.push({ ...this.newItem });
this.newItem = { name: '', price: 0, quantity: 1 };
},
removeItem(index) {
this.cartItems.splice(index, 1);
},
calculateTotal() {
return this.cartItems.reduce((total, item) => total + (item.price * item.quantity), 0);
}
}
});
```
此部分展示了如何利用 Vue 方法来管理购物车逻辑。`addItem` 和 `removeItem` 是用于增删商品的核心操作。
#### 4. 动态属性绑定 `:disabled`
为了增强用户体验,在某些情况下可能需要禁用按钮。例如,如果某个商品的数量减少至零,则应禁用其减号按钮。
```html
<button @click="decreaseQuantity(item)" :disabled="item.quantity === 0">-</button>
<input type="number" v-model.number="item.quantity" min="0"/>
<button @click="increaseQuantity(item)">+</button>
```
这里使用了 `:disabled` 属性绑定条件表达式 `item.quantity === 0`,从而控制按钮的状态。
#### 改进建议
- **性能优化**: 对于大型购物车应用,考虑分页加载或者虚拟滚动技术以提升页面响应速度。
- **状态管理**: 如果项目复杂度较高,推荐引入 Vuex 或 Pinia 进行全局状态管理。
- **表单验证**: 添加前端校验机制确保用户提交合法数据,比如防止负数或非法字符作为商品名。
```javascript
methods: {
decreaseQuantity(item) {
if (item.quantity > 0) {
item.quantity--;
}
},
increaseQuantity(item) {
item.quantity++;
}
}
```
以上改进增加了调整商品数量的功能,并且保持良好的交互体验。
---
阅读全文
相关推荐






<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>index.html</title> </head> <body>
首页 家乡简介 风景名胜 美食特色 登陆注册 登录 <form> <input type="text" placeholder="姓名"> <input type="password" placeholder="密码"> <input type="submit" value="登录"> 忘记密码? 点击这里 没有账户? 注册 </form> 安庆欢迎您 </body> </html>






