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.mzph.cn/news/887267.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

MFC 对话框中显示CScrollView实例

有时候我们需要在对话框中显示CScrollView效果的控件&#xff0c;类似于以下效果&#xff1a; 使用实例可参考&#xff1a;MFC对话框显示CScrollView例子_哔哩哔哩_bilibili 创建CScrollView中显示的子对话框与子类&#xff1a; 两个对话框对应的类&#xff1a; CScrollView继…

数仓工具—Hive语法之窗口函数中的 case when

窗口函数中的 case when 今天我们看一下窗口函数和case when 的各种花活,最近的需求各种窗口,一个需求中十几个窗口,加上各种条件边界,所以写了大量的窗口函数和case when的组合,今天我们来看一下。 我们的数据如下 %spark.pyspark df2 = spark.createDataFrame([(&quo…

高危,Laravel参数注入漏洞安全风险通告

今日&#xff0c;亚信安全CERT监控到安全社区研究人员发布安全通告&#xff0c;披露了Laravel 参数注入漏洞(CVE-2024-52301)。在受影响的版本中&#xff0c;Application.php 文件的 detectEnvironment 函数直接使用了 $_SERVER[argv]&#xff0c;但没有检查运行环境是否为 CLI…

【unity小技巧】unity常用的编辑器扩展

文章目录 1. **自定义 Inspector&#xff08;Inspector 面板&#xff09;**示例&#xff1a; 2. **Editor Window&#xff08;自定义编辑器窗口&#xff09;**示例&#xff1a; 3. **PropertyDrawer&#xff08;自定义属性绘制&#xff09;**示例&#xff1a; 4. **菜单项&…

数字化工厂 MES试点方案全解析(二)

生产过程监控与数据采集 在生产线上部署各类传感器、数据采集终端等设备&#xff0c;与 MES 系统相连&#xff0c;实时采集生产数据&#xff0c;如设备运行参数&#xff08;温度、压力、转速等&#xff09;、产品加工数据&#xff08;尺寸、重量、加工时间等&#xff09;、物料…

Flink整合Hudi及使用

1、jar包上传 上传jar包即可完成整合 # 1、将hudi-flink1.15-bundle-0.15.0.jar包上传到flink的lib目录下 /usr/local/soft/flink-1.15.3/lib-- 如果没有启动集群&#xff0c;直接忽略 # 2、重启flink的集群 yarn application -list yarn application -kill application_170…

Flink 常用问题及常用配置(有用)

一、Flink 常用问题及常用配置 参数 示例 说明 execution.checkpointing.interval 3min Checkpoint 触发间隔 state.backend rocksdb / filesystem 用于设置statebackend类型, 默认会以内存为statebackend(无法支持大状态) taskmanager.memory.jvm-overhead.max 204…

vue3(十九)-基础入门之vue-nuxt反向代理

一、反向代理 1、下载 nuxtjs/proxy 使用 npm npm install nuxtjs/proxy 或使用 yarn yarn add nuxtjs/proxy 2、配置 nuxt.config.js 文件 export default {modules: [nuxtjs/axios,nuxtjs/proxy],axios: {baseURL: /,proxy: true},proxy: {/api/: {target: https://i.maoya…

5G CPE与4G CPE的主要区别有哪些

什么是CPE&#xff1f; CPE是Customer Premise Equipment&#xff08;客户前置设备&#xff09;的缩写&#xff0c;也可称为Customer-side Equipment、End-user Equipment或On-premises Equipment。CPE通常指的是位于用户或客户处的网络设备或终端设备&#xff0c;用于连接用户…

【数据结构-队列】力扣232. 用栈实现队列

请你仅使用两个栈实现先入先出队列。队列应当支持一般队列支持的所有操作&#xff08;push、pop、peek、empty&#xff09;&#xff1a; 实现 MyQueue 类&#xff1a; void push(int x) 将元素 x 推到队列的末尾 int pop() 从队列的开头移除并返回元素 int peek() 返回队列开头…

推荐一个QDirStat基于 Qt 的目录统计工具

QDirStat 是一款基于 Qt 的目录统计工具&#xff0c;旨在帮助用户分析磁盘空间使用情况并进行清理。QDirStat的一些主要特点和功能&#xff1a; 跨平台兼容性&#xff1a;QDirStat 支持 Linux、BSD、Unix-like 系统以及 macOS&#xff0c;确保了广泛的用户基础。 直观的数据展…

C语言数学函数库<math.h>的常用函数讲解

math函数 一. 基础数学函数1. fabs(double x) — 绝对值2. fmod(double x, double y) — 余数3. pow(double x, double y) — 幂运算4. sqrt(double x) — 平方根5. cbrt(double x) — 立方根6. hypot(double x, double y) — 计算斜边 二. 对数和指数函数1. exp(double x) — …

【海思Hi3519DV500】双目网络相机套板硬件规划方案

Hi3519DV500双目网络相机套板是针对该芯片设计的一款 IP 编码板 PCBA&#xff0c;硬件接口支持双目sensor 接入&#xff0c;SDIO3.0 接口、USB2.0、USB3.0、UART 接口以及丰富的 IO 扩展应用&#xff0c;可根据各种使用场景设计相应扩展板&#xff0c;丰富外围接口&#xff0c;…

已阻止加载“http://localhost:8086/xxx.js”的模块,它使用了不允许的 MIME 类型 (“text/plain”)。

记录今天解决的一个小bug 在终端启动8080端口号监听后&#xff0c;打开网址http://localhost:8080&#xff0c;发现不能正确加载页面&#xff0c;打开检查-控制台&#xff0c;出现如下警告&#xff1a;已阻止加载“http://localhost:8086/xxx.js”的模块&#xff0c;它使用了不…

unity3d——基础篇小项目(开始界面)

示例代码&#xff1a; using System.Collections; using System.Collections.Generic; using UnityEngine;public class BeginPanel : BasePanel<BeginPanel> {public UIButton btnBegin;public UIButton btnRank;public UIButton btnSetting;public UIButton btnQuit; …

【AI日记】24.11.19 GraphRAG

【AI论文解读】【AI知识点】【AI小项目】【AI战略思考】【AI日记】 核心工作 1 内容&#xff1a;修改简历和投简历时间&#xff1a;1 小时结果&#xff1a;都失败了&#xff0c;不过我不会气馁&#xff0c;我会越挫越勇 核心工作 2 内容&#xff1a;学习微软的开源项目 Grap…

React Native的界面与交互

React Native (RN) 是一个由 Facebook 开发的开源框架&#xff0c;用于构建跨平台的移动应用程序。它允许开发者使用 JavaScript 和 React 来创建原生 iOS 和 Android 应用。RN 的出现极大地简化了移动应用的开发过程&#xff0c;使得开发者可以更快速、更高效地构建高质量的应…

时序预测 | Matlab实现PSO-Elman粒子群优化递归神经网络时间序列预测

时序预测 | Matlab实现PSO-Elman粒子群优化递归神经网络时间序列预测 目录 时序预测 | Matlab实现PSO-Elman粒子群优化递归神经网络时间序列预测效果一览基本介绍程序设计参考资料 效果一览 基本介绍 Matlab实现PSO-Elman粒子群优化递归神经网络时间序列预测&#xff08;完整源…

Let‘s Encrypt SSL证书:acmessl.cn申请免费3个月证书

目录 一、CA机构 二、Lets Encrypt特点 三、申请SSL 一、CA机构 ‌Lets Encrypt‌是一个由非营利组织Internet Security Research Group (ISRG)运营的证书颁发机构&#xff08;CA&#xff09;&#xff0c;旨在通过自动化和开放的方式为全球网站提供免费、可靠的SSL/TLS证书。…

丹摩征文活动|基于丹摩算力Llama3.1实现私有化部署的Chatbot

一、简介 Llama 3.1 模型介绍 Llama 3.1 是一种大型的、开源的语言生成模型&#xff0c;类似于OpenAI的GPT-3&#xff0c;但它是由Meta&#xff08;前Facebook&#xff09;推出的。Llama 3.1具备强大的自然语言处理能力&#xff0c;可以执行各种语言任务&#xff0c;如对话生…