学习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的详细错误信息中包含捕获的异常信息
-
- 文章 · 前端 | 3分钟前 |
- JS高手进阶!手把手教你搞定this指向
- 157浏览 收藏
-
- 文章 · 前端 | 4分钟前 |
- 用Vue.js造轮子!手把手教你搭建企业官网(超详细踩坑经验)
- 433浏览 收藏
-
- 文章 · 前端 | 7分钟前 |
- 手把手教你用Vuetransition组件实现酷炫过渡动画
- 432浏览 收藏
-
- 文章 · 前端 | 9分钟前 |
- HTML轻松搞定轮播图,手把手教你做carousel超简单教程
- 388浏览 收藏
-
- 文章 · 前端 | 15分钟前 |
- CSS中的span标签到底是什么?手把手教你搞定span元素
- 231浏览 收藏
-
- 文章 · 前端 | 21分钟前 | JavaScript 代码简化 策略模式 函数组合 ifelseif
- JS技巧|ifelseif判断太繁琐?这个骚操作让你代码瞬间简洁!
- 377浏览 收藏
-
- 文章 · 前端 | 23分钟前 |
- 用Vue.js轻松搞定旅游网站,手把手教你实现炫酷页面布局与交互
- 399浏览 收藏
-
- 文章 · 前端 | 28分钟前 |
- JS如何玩转虹膜识别?前端实现生物特征识别超详细教程
- 379浏览 收藏
-
- 文章 · 前端 | 34分钟前 | JavaScript 兼容性 navigator.userAgent 浏览器类型 特性检测
- JS判断浏览器种类的五种方法,快来看看你用过几种!
- 149浏览 收藏
-
- 文章 · 前端 | 36分钟前 |
- JS实现滚动加载更多数据,超简单教程分享
- 202浏览 收藏
-
- 文章 · 前端 | 38分钟前 |
- html中标签没你想的那么简单!用法+案例教学
- 138浏览 收藏
-
- 前端进阶之JavaScript设计模式
- 设计模式是开发人员在软件开发过程中面临一般问题时的解决方案,代表了最佳的实践。本课程的主打内容包括JS常见设计模式以及具体应用场景,打造一站式知识长龙服务,适合有JS基础的同学学习。
- 542次学习
-
- GO语言核心编程课程
- 本课程采用真实案例,全面具体可落地,从理论到实践,一步一步将GO核心编程技术、编程思想、底层实现融会贯通,使学习者贴近时代脉搏,做IT互联网时代的弄潮儿。
- 508次学习
-
- 简单聊聊mysql8与网络通信
- 如有问题加微信:Le-studyg;在课程中,我们将首先介绍MySQL8的新特性,包括性能优化、安全增强、新数据类型等,帮助学生快速熟悉MySQL8的最新功能。接着,我们将深入解析MySQL的网络通信机制,包括协议、连接管理、数据传输等,让
- 497次学习
-
- JavaScript正则表达式基础与实战
- 在任何一门编程语言中,正则表达式,都是一项重要的知识,它提供了高效的字符串匹配与捕获机制,可以极大的简化程序设计。
- 487次学习
-
- 从零制作响应式网站—Grid布局
- 本系列教程将展示从零制作一个假想的网络科技公司官网,分为导航,轮播,关于我们,成功案例,服务流程,团队介绍,数据部分,公司动态,底部信息等内容区块。网站整体采用CSSGrid布局,支持响应式,有流畅过渡和展现动画。
- 484次学习
-
- 茅茅虫AIGC检测
- 茅茅虫AIGC检测,湖南茅茅虫科技有限公司倾力打造,运用NLP技术精准识别AI生成文本,提供论文、专著等学术文本的AIGC检测服务。支持多种格式,生成可视化报告,保障您的学术诚信和内容质量。
- 31次使用
-
- 赛林匹克平台(Challympics)
- 探索赛林匹克平台Challympics,一个聚焦人工智能、算力算法、量子计算等前沿技术的赛事聚合平台。连接产学研用,助力科技创新与产业升级。
- 54次使用
-
- 笔格AIPPT
- SEO 笔格AIPPT是135编辑器推出的AI智能PPT制作平台,依托DeepSeek大模型,实现智能大纲生成、一键PPT生成、AI文字优化、图像生成等功能。免费试用,提升PPT制作效率,适用于商务演示、教育培训等多种场景。
- 64次使用
-
- 稿定PPT
- 告别PPT制作难题!稿定PPT提供海量模板、AI智能生成、在线协作,助您轻松制作专业演示文稿。职场办公、教育学习、企业服务全覆盖,降本增效,释放创意!
- 59次使用
-
- Suno苏诺中文版
- 探索Suno苏诺中文版,一款颠覆传统音乐创作的AI平台。无需专业技能,轻松创作个性化音乐。智能词曲生成、风格迁移、海量音效,释放您的音乐灵感!
- 63次使用
-
- 优化用户界面体验的秘密武器:CSS开发项目经验大揭秘
- 2023-11-03 501浏览
-
- 使用微信小程序实现图片轮播特效
- 2023-11-21 501浏览
-
- 解析sessionStorage的存储能力与限制
- 2024-01-11 501浏览
-
- 探索冒泡活动对于团队合作的推动力
- 2024-01-13 501浏览
-
- UI设计中为何选择绝对定位的智慧之道
- 2024-02-03 501浏览