学习VUE3:创建一个基本的购物车应用示例
本篇文章向大家介绍《学习VUE3:创建一个基本的购物车应用示例》,主要包括,具有一定的参考价值,需要的朋友可以参考一下。
随着现代 Web 应用程序的日益普及,Vue 成为了一款非常流行的前端框架。不久前,Vue 的最新版本 Vue3 也发布了。Vue3 带来了一些重要的改进和新特性,如更好的性能、更好的开发体验等。本篇文章将带你进入 VUE3 的世界,构建一个简单的购物车应用程序。
功能列表
在建立实例之前,让我们先看一下我们的购物车应用程序需要实现的功能列表:
- 实现一个商品列表,包括商品名称、商品图片、商品价格等信息
- 用户可以通过点击商品将其添加到购物车中
- 用户可以在购物车中查看购物商品列表,包括商品名称、商品数量、商品价格等信息
- 用户可以增加或减少购物车中每个商品的数量,并计算出购物车中商品总价
- 用户可以删除购物车中的商品
实例构建
在本例中,我们将使用 Vue CLI 来创建项目,这是一个非常流行的脚手架工具,可以帮助我们快速构建 Vue 项目。
创建项目
首先,需要安装 Vue CLI,可以通过以下命令进行安装:
npm install -g @vue/cli
安装完成后,我们可以使用以下命令来创建我们的项目:
vue create shopping-cart
在安装过程中,它将提示你选择一些选项,你可以选择默认选项或根据自己的需求进行定制。安装完成后,进入项目目录,并启动客户端服务器:
cd shopping-cart npm run serve
添加商品列表
我们将使用以下示例商品列表,包括商品名称、商品图片、商品描述、商品价格等信息:
const products = [ { name: '可乐', description: '碳酸软饮料', image: 'https://cdn.pixabay.com/photo/2018/05/08/08/44/coca-cola-3388554_960_720.jpg', price: 2, }, { name: '红牛', description: '功能性饮料', image: 'https://cdn.pixabay.com/photo/2020/06/17/08/57/red-bull-5317749_960_720.jpg', price: 3, }, { name: '雪碧', description: '柠檬味碳酸饮料', image: 'https://cdn.pixabay.com/photo/2016/03/09/09/27/beverage-1245692_960_720.jpg', price: 2.5, }, ];
要在应用程序中显示此产品列表,我们需要在 src/components
目录中创建一个名为 ProductList.vue
的新组件。这个组件将包含所有产品,并针对每个产品显示一个卡片元素。以下是组件的代码:
<template> <div class="product-list"> <div class="card" v-for="product in products" :key="product.name"> <div class="card-image"> <figure class="image is-4by3"> <img :src="product.image" alt="Product Image"> </figure> </div> <div class="card-content"> <div class="media"> <div class="media-content"> <p class="title is-4">{{ product.name }}</p> <p class="subtitle is-6">{{ product.description }}</p> </div> </div> <div class="content"> <p><strong>{{ product.price }}</strong></p> <button class="button is-primary" @click="addProduct(product)">Add to Cart</button> </div> </div> </div> </div> </template> <script> export default { name: 'ProductList', props: { products: { type: Array, required: true, }, }, methods: { addProduct(product) { this.$emit('add-to-cart', product); }, }, }; </script> <style scoped> .product-list .card { margin-bottom: 20px; } </style>
这个组件包含一个 div
元素,类名为 product-list
,内部包含多个商品卡片元素。每个卡片元素都有一个图片、一个标题、一段简短的描述和一个加入购物车按钮。当单击“添加到购物车”按钮时,该方法将触发“add-to-cart”事件,并发送商品对象作为参数。
添加购物车组件
接下来,我们将创建购物车组件。我们将使用另一个名为 ShoppingCart.vue
的单文件组件,此组件将显示用户已添加到购物车中的所有商品。
<template> <div class="shopping-cart"> <div v-if="items.length === 0" class="empty-cart-message"> Your cart is empty </div> <div v-else> <div class="cart-items"> <div class="cart-item" v-for="(item, index) in items" :key="index"> <div class="cart-item-image"> <img :src="item.product.image" alt="Product Image"> </div> <div class="cart-item-details"> <div class="cart-item-name">{{ item.product.name }}</div> <div class="cart-item-quantity"> <button class="button is-small" @click="decrementQuantity(item)">-</button> <span>{{ item.quantity }}</span> <button class="button is-small" @click="incrementQuantity(item)">+</button> </div> <div class="cart-item-price">{{ item.product.price }}</div> <button class="delete" @click="removeFromCart(item)"></button> </div> </div> </div> <div class="cart-summary"> <div class="cart-total"> <span class="total-text">Total:</span> <span class="total-amount">{{ total }}</span> </div> <div class="cart-actions"> <button class="button is-link">Checkout</button> </div> </div> </div> </div> </template> <script> export default { name: 'ShoppingCart', props: { items: { type: Array, required: true, }, }, computed: { total() { return this.items.reduce((acc, item) => acc + item.product.price * item.quantity, 0); }, }, methods: { removeFromCart(item) { this.$emit('remove-from-cart', item); }, incrementQuantity(item) { item.quantity += 1; }, decrementQuantity(item) { item.quantity -= 1; if (item.quantity <= 0) { this.removeFromCart(item); } }, }, }; </script> <style scoped> .shopping-cart .cart-item { display: flex; margin-bottom: 1rem; } .shopping-cart .cart-item-image { flex: 1; margin-right: 1rem; min-width: 10rem; } .shopping-cart .cart-item-image img { width: 100%; } .shopping-cart .cart-item-details { flex: 2; display: flex; flex-direction: column; } .shopping-cart .cart-item-name { margin-bottom: 0.5rem; } .shopping-cart .cart-item-quantity { margin-bottom: 0.5rem; display: flex; align-items: center; } .shopping-cart .cart-item-quantity button { margin-right: 0.5rem; } .shopping-cart .cart-item-price { margin-bottom: 0.5rem; font-weight: bold; font-size: 1.25rem; } .shopping-cart .cart-summary { margin-top: 1rem; } .shopping-cart .cart-total { display: flex; align-items: center; } .shopping-cart .total-text { margin-right: 1rem; } .shopping-cart .total-amount { font-weight: bold; font-size: 1.25rem; margin-right: 1rem; } </style>
购物车组件包含一个 items
属性,表示已添加到购物车中的商品数组。如果列表为空,则显示“您的购物车是空的”消息。否则,它将显示包含所有购物车商品的列表,并显示总价和一个结账按钮。每个购物车项目元素都包含购物车商品图像、姓名、数量和价格,并且用户可以通过加、减或删除按钮来操作购物车的商品数量。
添加 App 组件
完整的应用程序将使用两个组件: ProductList.vue
和 ShoppingCart.vue
。我们将使用 App.vue 作为主组件,将两个子组件放置在其中。
<template> <div id="app"> <div class="container"> <div class="columns"> <div class="column"> <product-list :products="products" @add-to-cart="addToCart"></product-list> </div> <div class="column"> <shopping-cart :items="items" @remove-from-cart="removeFromCart"></shopping-cart> </div> </div> </div> </div> </template> <script> import ProductList from './components/ProductList.vue'; import ShoppingCart from './components/ShoppingCart.vue'; export default { name: 'App', components: { ProductList, ShoppingCart, }, data() { return { products: [ // products data goes here ], items: [], }; }, methods: { addToCart(product) { const item = this.items.find((item) => item.product.name === product.name); if (item) { item.quantity += 1; } else { this.items.push({ product, quantity: 1, }); } }, removeFromCart(item) { const index = this.items.findIndex((i) => i.product.name === item.product.name); if (index !== -1) { this.items.splice(index, 1); } }, }, }; </script> <style> .container { margin-top: 50px; } </style>
主要 App.vue 组件包含两个子组件: ProductList.vue
和 ShoppingCart.vue
。在 data
属性中,它还包含一个产品数组和购物车项目数组,以及可用于向购物车添加物品或从购物车删除物品的方法。
通过以上代码,我们已经成功的实现了一个简单的购物车应用程序。虽然这个应用程序只是一个小示例,但它展示了如何使用 Vue3 构建具有重要功能的应用程序。如果你想学习更多 Vue3 的相关内容,建议查看 Vue3 官方文档。
本篇关于《学习VUE3:创建一个基本的购物车应用示例》的介绍就到此结束啦,但是学无止境,想要了解学习更多关于文章的相关知识,请关注golang学习网公众号!

- 上一篇
- 如何在PHP语言开发中避免使用过时或错误的函数?

- 下一篇
- 如何在Java的详细错误信息中包含捕获的异常信息
-
- 文章 · 前端 | 43秒前 |
- HTML5折叠标签使用教程:details和summary详解
- 270浏览 收藏
-
- 文章 · 前端 | 51秒前 |
- BOM中如何分析用户颜色偏好?
- 336浏览 收藏
-
- 文章 · 前端 | 1分钟前 |
- JS数组反转方法全解析
- 370浏览 收藏
-
- 文章 · 前端 | 2分钟前 |
- 事件循环影响性能,掌握循环优化技巧
- 309浏览 收藏
-
- 文章 · 前端 | 6分钟前 |
- 表单验证样式设计技巧
- 450浏览 收藏
-
- 文章 · 前端 | 6分钟前 |
- HTML嵌入地图教程|GoogleMaps集成方法
- 443浏览 收藏
-
- 文章 · 前端 | 9分钟前 |
- JavaScriptmap方法全解析
- 143浏览 收藏
-
- 文章 · 前端 | 12分钟前 |
- 事件循环不阻塞主线程的技巧
- 155浏览 收藏
-
- 文章 · 前端 | 15分钟前 |
- ChromeV3扩移:单线程与脚本注入实战
- 474浏览 收藏
-
- 文章 · 前端 | 18分钟前 |
- Vue.js开发企业官网项目实战经验分享
- 212浏览 收藏
-
- 文章 · 前端 | 24分钟前 |
- JavaScript对象解构赋值全解析
- 348浏览 收藏
-
- 前端进阶之JavaScript设计模式
- 设计模式是开发人员在软件开发过程中面临一般问题时的解决方案,代表了最佳的实践。本课程的主打内容包括JS常见设计模式以及具体应用场景,打造一站式知识长龙服务,适合有JS基础的同学学习。
- 542次学习
-
- GO语言核心编程课程
- 本课程采用真实案例,全面具体可落地,从理论到实践,一步一步将GO核心编程技术、编程思想、底层实现融会贯通,使学习者贴近时代脉搏,做IT互联网时代的弄潮儿。
- 511次学习
-
- 简单聊聊mysql8与网络通信
- 如有问题加微信:Le-studyg;在课程中,我们将首先介绍MySQL8的新特性,包括性能优化、安全增强、新数据类型等,帮助学生快速熟悉MySQL8的最新功能。接着,我们将深入解析MySQL的网络通信机制,包括协议、连接管理、数据传输等,让
- 498次学习
-
- JavaScript正则表达式基础与实战
- 在任何一门编程语言中,正则表达式,都是一项重要的知识,它提供了高效的字符串匹配与捕获机制,可以极大的简化程序设计。
- 487次学习
-
- 从零制作响应式网站—Grid布局
- 本系列教程将展示从零制作一个假想的网络科技公司官网,分为导航,轮播,关于我们,成功案例,服务流程,团队介绍,数据部分,公司动态,底部信息等内容区块。网站整体采用CSSGrid布局,支持响应式,有流畅过渡和展现动画。
- 484次学习
-
- 千音漫语
- 千音漫语,北京熠声科技倾力打造的智能声音创作助手,提供AI配音、音视频翻译、语音识别、声音克隆等强大功能,助力有声书制作、视频创作、教育培训等领域,官网:https://qianyin123.com
- 122次使用
-
- MiniWork
- MiniWork是一款智能高效的AI工具平台,专为提升工作与学习效率而设计。整合文本处理、图像生成、营销策划及运营管理等多元AI工具,提供精准智能解决方案,让复杂工作简单高效。
- 119次使用
-
- NoCode
- NoCode (nocode.cn)是领先的无代码开发平台,通过拖放、AI对话等简单操作,助您快速创建各类应用、网站与管理系统。无需编程知识,轻松实现个人生活、商业经营、企业管理多场景需求,大幅降低开发门槛,高效低成本。
- 133次使用
-
- 达医智影
- 达医智影,阿里巴巴达摩院医疗AI创新力作。全球率先利用平扫CT实现“一扫多筛”,仅一次CT扫描即可高效识别多种癌症、急症及慢病,为疾病早期发现提供智能、精准的AI影像早筛解决方案。
- 128次使用
-
- 智慧芽Eureka
- 智慧芽Eureka,专为技术创新打造的AI Agent平台。深度理解专利、研发、生物医药、材料、科创等复杂场景,通过专家级AI Agent精准执行任务,智能化工作流解放70%生产力,让您专注核心创新。
- 129次使用
-
- 优化用户界面体验的秘密武器:CSS开发项目经验大揭秘
- 2023-11-03 501浏览
-
- 使用微信小程序实现图片轮播特效
- 2023-11-21 501浏览
-
- 解析sessionStorage的存储能力与限制
- 2024-01-11 501浏览
-
- 探索冒泡活动对于团队合作的推动力
- 2024-01-13 501浏览
-
- UI设计中为何选择绝对定位的智慧之道
- 2024-02-03 501浏览