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

项目实战:Vue3开发一个购物车

 

        这段HTML代码实现了一个简单的购物车实战小项目的前端页面,结合了Vue.js框架来实现数据响应式和交互逻辑。页面展示了购物车中的商品项,每个商品项有增减数量的按钮,并且能显示商品总数以及目前固定为0元的商品总价和总价计算。 【运用响应式数据内容】

<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><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: 25px;}.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" v-model="cartItems[index].unit_price"/> 元/斤 <br> 单价:{{cartItems[index].unit_price}} 元/斤</p></div></div><!-- 提示:可以用计算属性或数据变动侦听器,跟踪商品数和单价的变化,进而求出总数和总价--><h3>商品总数:  <ins> {{totalItems}} </ins> 件</h3><h3>商品总价:  <ins> {{sum}}</ins> 元</h3></div><script type="module">import { createApp, reactive, computed } from './vue.esm-browser.js'createApp({setup() {// 1.定义属性:存储商品的(响应式)数组 const cartItems = reactive([{ name: '苹果', quantity: 0, unit_price: "" },{ name: '香蕉', quantity: 0, unit_price: "" },{ name: '菠萝', quantity: 0, unit_price: "" },// 可以自适应添加更多商品 // { name: '芒果', quantity: 0, unit_price: "" },// { name: '鸭梨', quantity: 0, unit_price: "" },]);// 2.定义方法:增加商品数// increaseQuantity方法:以一个商品在cartItems数组中的索引为参数const increaseQuantity = (index) => {// 点击"+"时,商品数量+1cartItems[index].quantity += 1;  }// 3.定义方法:减少商品数const decreaseQuantity = (index) => {// 商品数量没有负值,因此多加一道判断// 商品数量小于0时,不会-1if(cartItems[index].quantity > 0){// 点击"-"时,商品数量-1cartItems[index].quantity -= 1;}}// 4.定义方法:计算商品总数// totalItems计算属性const totalItems = computed(() => {let total_items = 0;// 遍历cartItems数组,计算每个商品的数量for(const item of cartItems){// 累加在一起total_items += item.quantity;}return total_items})// 5.定义方法:计算商品总价const sum = computed(() =>{let total_sum=0;for(const item of cartItems){// 商品总价=商品数量*商品单价total_sum+=item.quantity*item.unit_price;}return total_sum})// 6.暴露属性和方法return {cartItems, increaseQuantity,decreaseQuantity,totalItems,sum};},}).mount('#app');</script>
</body>
</html>

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

相关文章:

  • Oracle SQL*Plus中的SET VERIFY
  • Spring AI 框架使用的核心概念
  • 二叉树路径相关算法题|带权路径长度WPL|最长路径长度|直径长度|到叶节点路径|深度|到某节点的路径非递归(C)
  • 前端:JavaScript (学习笔记)【2】
  • [面试]-golang基础面试题总结
  • 【案例】泛微.齐业成助力北京中远大昌汽车实现数电票全流程管理
  • 微软安全文章合集
  • 自然语言处理: RAG优化之Embedding模型选型重要依据:mteb/leaderboard榜
  • 鸿蒙主流路由详解
  • C#构建一个简单的循环神经网络,模拟对话
  • Linux上安装单机版Kibana6.8.1
  • 短视频矩阵矩阵,矩阵号策略
  • Rust 力扣 - 2266. 统计打字方案数
  • 【大数据技术与开发实训】携程景点在线评论分析
  • 46.坑王驾到第十期:vscode 无法使用 tsc 命令
  • postman 调用 下载接口(download)使用默认名称(response.txt 或随机名称)
  • 单片机_简单AI模型训练与部署__从0到0.9
  • 对撞双指针(七)三数之和
  • 【Ubuntu24.04】服务部署(虚拟机)
  • timm库加载的模型可视化
  • 服务限流、降级、熔断-SpringCloud
  • 2024最新YT-DLP使用demo网页端渲染
  • 《第十部分》1.STM32之通信接口《精讲》之IIC通信---介绍
  • wireshark使用lua解析自定义协议
  • (Keil)MDK-ARM各种优化选项详细说明、实际应用及拓展内容
  • Qt实现可拖拽的矩形
  • CentOS:A服务器主动给B服务器推送(上传),B服务器下载A服务器文件(下载)
  • Oracle 执行计划查看方法汇总及优劣对比
  • TCL大数据面试题及参考答案
  • 九、FOC原理详解