当前位置: 首页 > news >正文

vue--制作购物车

🤔如何制作出下列效果呢?👇

😶‍🌫️首先:

设置css样式:

 <style>body {font-family: Arial, sans-serif;}.cart-item {width: 50%;margin-bottom: 15px;padding: 10px;border: 2px solid gray;border-radius: 10px;background-color: #ddd;}.buttons {margin-top: 5px;}.buttons button {padding: 5px 10px;margin-right: 5px;font-size: 16px;cursor: pointer;border: none;border-radius: 3px;background-color: pink;}.buttons input {width: 15px;}.buttons button:hover {background-color: yellow;}.quantity {font-size: 18px;font-weight: bold;margin-left: 10px;}h1, h2 {color: #333;}</style>
</head>
<body>

 🤓接下来:

1、可以使用v-for指令,假设有n个品类,则生成n个商品项

2、使用下标指令遍历商品的名字和遍历商品的价格

3、v-on事件绑定设置+、- 数量

 <!-- 提示:可以使用v-for指令,假设有n个品类,则生成n个商品项--><div class="cart-item" v-for="(item, index) in cartItems"><div class="buttons"><span>{{item.name}} &nbsp;&nbsp;</span><button v-on:click = "decreaseQuantity(index)">-</button><span class="quantity">{{ cartItems[index].quantity }}&nbsp;&nbsp;</span><button v-on:click = "increaseQuantity(index)">+</button><p>请输入价格:<input type="text" /> 元/斤 <br> 单价:1 元/斤</p></div></div><!-- 提示:可以用计算属性或数据变动侦听器,跟踪商品数和单价的变化,进而求出总数和总价--><h3>商品总数:  <ins> {{totalItems}} </ins> 件</h3><h3>商品总价:  <ins> 1 </ins> 元</h3></div>

2、🧐定义属性:

  // 1.定义属性:存储商品的(响应式)数组 const cartItems = reactive([{ name: '苹果', quantity: 1, unit_price: 1 },{ name: '香蕉', quantity: 1, unit_price: 1 },{ name: '菠萝', quantity: 1, unit_price: 1 },

3、🧐定义方法--增减数量

 // 2.定义方法:增加商品数const increaseQuantity = (index) => {cartItems[index].quantity += 1;  }// 3.定义方法:减少商品数const decreaseQuantity = (index) => {if(cartItems[index].quantity > 1){cartItems[index].quantity -= 1;}}

4、🧐定义商品数量:

const totalItems = computed(() => {let total_items = 0;for(const item of cartItems){total_items += item.quantity;}return total_items})

😲最后返回函数:

  // 6.暴露属性和方法return {cartItems, increaseQuantity,decreaseQuantity,totalItems};},}).mount('#app');</script>

完整代码:👇

 <title>实战小项目:购物车</title><style>body {font-family: Arial, sans-serif;}.cart-item {width: 50%;margin-bottom: 15px;padding: 10px;border: 2px solid gray;border-radius: 10px;background-color: #ddd;}.buttons {margin-top: 5px;}.buttons button {padding: 5px 10px;margin-right: 5px;font-size: 16px;cursor: pointer;border: none;border-radius: 3px;background-color: pink;}.buttons input {width: 15px;}.buttons button:hover {background-color: yellow;}.quantity {font-size: 18px;font-weight: bold;margin-left: 10px;}h1, h2 {color: #333;}</style>
</head>
<body><div id="app"><h1>实战小项目:购物车</h1><!-- 提示:可以使用v-for指令,假设有n个品类,则生成n个商品项--><div class="cart-item" v-for="(item, index) in cartItems"><div class="buttons"><span>{{item.name}} &nbsp;&nbsp;</span><button v-on:click = "decreaseQuantity(index)">-</button><span class="quantity">{{ cartItems[index].quantity }}&nbsp;&nbsp;</span><button v-on:click = "increaseQuantity(index)">+</button><p>请输入价格:<input type="text" /> 元/斤 <br> 单价:1 元/斤</p></div></div><!-- 提示:可以用计算属性或数据变动侦听器,跟踪商品数和单价的变化,进而求出总数和总价--><h3>商品总数:  <ins> {{totalItems}} </ins> 件</h3><h3>商品总价:  <ins> 1 </ins> 元</h3></div><script type="module">import { createApp, reactive, computed } from './vue.esm-browser.js'createApp({setup() {// 1.定义属性:存储商品的(响应式)数组 const cartItems = reactive([{ name: '苹果', quantity: 1, unit_price: 1 },{ name: '香蕉', quantity: 1, unit_price: 1 },{ name: '菠萝', quantity: 1, unit_price: 1 },]);// 2.定义方法:增加商品数const increaseQuantity = (index) => {cartItems[index].quantity += 1;  }// 3.定义方法:减少商品数const decreaseQuantity = (index) => {if(cartItems[index].quantity > 1){cartItems[index].quantity -= 1;}}// 4.定义方法:计算商品总数const totalItems = computed(() => {let total_items = 0;for(const item of cartItems){total_items += item.quantity;}return total_items})// 5.定义方法:计算商品总价// 6.暴露属性和方法return {cartItems, increaseQuantity,decreaseQuantity,totalItems};},}).mount('#app');</script>
</body>
</html>

http://www.lryc.cn/news/490773.html

相关文章:

  • 深度测评腾讯云 HAI 智算服务:高性能 AI 计算的新标杆
  • MQ重复消费与消息顺序
  • 应用商店双弹窗“APP在向用户申请权限时未同步告知用户申请此权限的理由”驳回uni-app应用上线的解决方法
  • 第 32 章 - Go语言 部署与运维
  • 噪杂环境离线语音通断器效果展示
  • 【django】扩展
  • 逆向破解识别基础
  • MFC 下拉框显示问题和控件自适应窗口大小
  • Docker 的存储驱动及其优缺点
  • 单片机系统的性能指标有哪些?
  • 波点音乐自动点击
  • HTTP 消息结构
  • ESP32学习笔记_Bluetooth(1)——蓝牙技术与 BLE 通信机制简介
  • C0034.在Ubuntu中安装的Qt路径
  • jdbc学习——SQL注入
  • JavaEE-线程安全专题
  • Android 设备使用 Wireshark 工具进行网络抓包
  • 物联网无线局域网WiFi开发(一):WiFi智能家居解决方案
  • GMAN解读(论文+代码)
  • 速盾:ddos防御手段哪种比较好?高防cdn怎么样?
  • Spring:AOP切入点表达式
  • 《文件操作》
  • python特殊字符序列
  • 卷积神经网络(CNN)中的批量归一化层(Batch Normalization Layer)
  • LLaMA-Mesh: Unifying 3D Mesh Generation with Language Models 论文解读
  • 【ESP32CAM+Android+C#上位机】ESP32-CAM在STA或AP模式下基于UDP与手机APP或C#上位机进行视频流/图像传输
  • ESP-KeyBoard:基于 ESP32-S3 的三模客制化机械键盘
  • 28.UE5游戏框架,事件分发器,蓝图接口
  • Puppeteer 和 Cheerio 在 Node.js 中的应用
  • Unity2D 关于N方向俯视角 中 角色移动朝向的问题