Vue.js 2 项目实战(五):水果购物车
前言Vue.js 是一个用于构建用户界面的渐进式 JavaScript 框架。它的计划目标是通过采用易于上手的布局和强大的功能,使前端开发变得更加简便和高效。以下是 Vue.js 的一些关键特性和优点:
核心特性
[*] 声明式渲染
[*]Vue.js 使用声明式语法来描述用户界面,数据和视图是双向绑定的。当数据变革时,视图会自动更新。
[*] 组件系统
[*]Vue.js 提供了一个机动的组件系统,允许开发者将 UI 分解成可复用的组件,每个组件都有自己的逻辑和样式。
[*] 单文件组件 (SFC)
[*]Vue.js 允许开发者将 HTML、CSS 和 JavaScript 放在同一个 .vue 文件中,如许可以更容易地管理组件。
[*] 虚拟 DOM
[*]Vue.js 使用虚拟 DOM 来优化更新过程,通过最小化现实 DOM 操纵来进步性能。
[*] 反应式数据绑定
[*]Vue.js 提供了响应式的数据绑定系统,使得数据与 DOM 之间的同步变得简单和高效。
[*] 指令
[*]Vue.js 提供了一组内置指令(如 v-bind、v-model 和 v-for),帮助开发者轻松地操纵 DOM。
优点
[*] 易于上手
[*]Vue.js 的学习曲线较低,适合新手入门,而且文档详细、社区活跃。
[*] 机动性
[*]Vue.js 可以与现有项目集成,也可以用于构建复杂的单页面应用(SPA)。
[*] 性能高效
[*]得益于虚拟 DOM 和响应式系统,Vue.js 在处理大量数据更新时表现出色。
[*] 生态系统
[*]Vue.js 拥有丰富的生态系统,包罗 Vue Router、Vuex、Vue CLI 等工具和库,支持开发者在不同场景下使用。
[*] 强大的社区支持
[*]Vue.js 有一个环球活跃的社区,提供了大量的插件、教程和支持资源。
知识点
1. v-if v-else 条件渲染,如果没有数据则显示空车
2. v-for 循环遍历渲染数组
3. v-bind 绑定 class 设置条件
4. v-model 绑定属性
5. v-bind 绑定 src
6. 插值语法
7. @click 点击事件实现数量加减
8. filter() 方法更新数组达到删除元素的效果
9. find() 方法通过 id 找到对应的水果
10. :disabled 动态属性绑定限制用户
11. 盘算属性完备写法实现全选的两个功能
12. every() 方法检查每一个元素
13. foreach() 方法设置每一个元素
14. some() 方法实现如果有恣意一个按钮不是 true,则全选,反之取消全部选择
15. reduce() 方法盘算总价与数量
16. 判定条件实现没有选中的则不添加
项目效果
初始页面
https://i-blog.csdnimg.cn/direct/d4a5884ba65d4c36be647c5c47eae55a.png
添加水果
https://i-blog.csdnimg.cn/direct/5299d10aa0d94678ba209f6f5cbe4f19.png
全选按钮
https://i-blog.csdnimg.cn/direct/35395737af584bc3aef5aca9fd442ef8.png
https://i-blog.csdnimg.cn/direct/1e29ed1e41a748fe8a29bd4b3637c061.png
删除水果
https://i-blog.csdnimg.cn/direct/0092f661acf3466d9cbb90533aa52f72.png
当地缓存
https://i-blog.csdnimg.cn/direct/49d8662e82134f17aaf333d5505365a1.png
源代码
index.css
.app-container {
padding-bottom: 300px;
width: 800px;
margin: 0 auto;
}
@media screen and (max-width: 800px) {
.app-container {
width: 600px;
}
}
.app-container .banner-box {
border-radius: 20px;
overflow: hidden;
margin-bottom: 10px;
}
.app-container .banner-box img {
width: 100%;
}
.app-container .nav-box {
background: #ddedec;
height: 60px;
border-radius: 10px;
padding-left: 20px;
display: flex;
align-items: center;
}
.app-container .nav-box .my-nav {
display: inline-block;
background: #5fca71;
border-radius: 5px;
width: 90px;
height: 35px;
color: white;
text-align: center;
line-height: 35px;
margin-right: 10px;
}
.breadcrumb {
font-size: 16px;
color: gray;
}
.table {
width: 100%;
text-align: left;
border-radius: 2px 2px 0 0;
border-collapse: separate;
border-spacing: 0;
}
.th {
color: rgba(0, 0, 0, 0.85);
font-weight: 500;
text-align: left;
background: #fafafa;
border-bottom: 1px solid #f0f0f0;
transition: background 0.3s ease;
}
.th.num-th {
flex: 1.5;
}
.th {
text-align: center;
}
.th:nth-child(4),
.th:nth-child(5),
.th:nth-child(6),
.th:nth-child(7) {
text-align: center;
}
.th.th-pic {
flex: 1.3;
}
.th:nth-child(6) {
flex: 1.3;
}
.th,
.td {
position: relative;
padding: 16px 16px;
overflow-wrap: break-word;
flex: 1;
}
.pick-td {
font-size: 14px;
}
.main,
.empty {
border: 1px solid #f0f0f0;
margin-top: 10px;
}
.tr {
display: flex;
cursor: pointer;
border-bottom: 1px solid #ebeef5;
}
.tr.active {
background-color: #f5f7fa;
}
.td {
display: flex;
justify-content: center;
align-items: center;
}
.table img {
width: 100px;
height: 100px;
}
button {
outline: 0;
box-shadow: none;
color: #fff;
background: #d9363e;
border-color: #d9363e;
color: #fff;
background: #d9363e;
border-color: #d9363e;
line-height: 1.5715;
position: relative;
display: inline-block;
font-weight: 400;
white-space: nowrap;
text-align: center;
background-image: none;
border: 1px solid transparent;
box-shadow: 0 2px 0 rgb(0 0 0 / 2%);
cursor: pointer;
transition: all 0.3s cubic-bezier(0.645, 0.045, 0.355, 1);
-webkit-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
touch-action: manipulation;
height: 32px;
padding: 4px 15px;
font-size: 14px;
border-radius: 2px;
}
button.pay {
background-color: #3f85ed;
margin-left: 20px;
}
.bottom {
height: 60px;
display: flex;
align-items: center;
justify-content: space-between;
padding-right: 20px;
border: 1px solid #f0f0f0;
border-top: none;
padding-left: 20px;
}
.right-box {
display: flex;
align-items: center;
}
.check-all {
cursor: pointer;
}
.price {
color: hotpink;
font-size: 30px;
font-weight: 700;
}
.price-box {
display: flex;
align-items: center;
}
.empty {
padding: 20px;
text-align: center;
font-size: 30px;
color: #909399;
}
.my-input-number {
display: flex;
}
.my-input-number button {
height: 40px;
color: #333;
border: 1px solid #dcdfe6;
background-color: #f5f7fa;
}
.my-input-number button:disabled {
cursor: not-allowed!important;
}
.my-input-number .my-input__inner {
height: 40px;
width: 50px;
padding: 0;
border: none;
border-top: 1px solid #dcdfe6;
border-bottom: 1px solid #dcdfe6;
inputnumber.css
.my-input-number {
position: relative;
display: inline-block;
width: 140px;
line-height: 38px;
}
.my-input-number span {
-moz-user-select: none;
-webkit-user-select: none;
-ms-user-select: none;
}
.my-input-number .my-input {
display: block;
position: relative;
font-size: 14px;
width: 100%;
}
.my-input-number .my-input__inner {
-webkit-appearance: none;
background-color: #fff;
background-image: none;
border-radius: 4px;
border: 1px solid #dcdfe6;
box-sizing: border-box;
color: #606266;
display: inline-block;
font-size: inherit;
height: 40px;
line-height: 40px;
outline: none;
padding: 0 15px;
transition: border-color 0.2s cubic-bezier(0.645, 0.045, 0.355, 1);
width: 100%;
padding-left: 50px;
padding-right: 50px;
text-align: center;
}
.my-input-number .my-input-number__decrease,
.my-input-number .my-input-number__increase {
position: absolute;
z-index: 1;
top: 1px;
width: 40px;
height: auto;
text-align: center;
background: #f5f7fa;
color: #606266;
cursor: pointer;
font-size: 13px;
}
.my-input-number .my-input-number__decrease {
left: 1px;
border-radius: 4px 0 0 4px;
border-right: 1px solid #dcdfe6;
}
.my-input-number .my-input-number__increase {
right: 1px;
border-radius: 0 4px 4px 0;
border-left: 1px solid #dcdfe6;
}
.my-input-number .my-input-number__decrease.is-disabled,
.my-input-number .my-input-number__increase.is-disabled {
color: #c0c4cc;
cursor: not-allowed;
}
html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="stylesheet" href="./css/inputnumber.css" />
<link rel="stylesheet" href="./css/index.css" />
<title>购物车</title>
</head>
<body>
<div class="app-container" id="app">
<!-- 顶部banner -->
<div class="banner-box"><img src="./img/fruit.jpg" alt="Fruit"></div>
<!-- 面包屑 -->
<div class="breadcrumb">
<span>
页:
[1]