该组件适用于电子商务网站或任何需要管理购物清单的应用程序。它提供了一个交互式界面,允许用户添加、删除和更新购物清单中的商品。
该组件具有以下基本功能:
1. 添加商品
添加商品功能通过 add()
方法实现,该方法将新商品对象添加到 items
数组中。
add() {
this.items.push({
name: this.newItemName,
price: this.newItemPrice,
quantity: 1
});
}
2. 删除商品
删除商品功能通过 remove()
方法实现,该方法从 items
数组中删除指定索引的商品。
remove(index) {
this.items.splice(index, 1);
}
3. 更新商品数量
更新商品数量功能通过 updateQuantity()
方法实现,该方法更新指定索引的商品数量。
updateQuantity(index, quantity) {
this.items[index].quantity = quantity;
}
4. 计算总价
计算总价功能通过 calculateTotalPrice()
方法实现,该方法遍历 items
数组并累加每个商品的价格乘以其数量。
calculateTotalPrice() {
let total = 0;
for (let item of this.items) {
total += item.price * item.quantity;
}
return total;
}
开发经验与收获:
v-for
循环渲染动态列表。未来拓展与优化: