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

07-Vue基础之综合案例——小黑记事本

个人名片:
😊作者简介:一名大二在校生
🤡 个人主页:坠入暮云间x
🐼
座右铭:懒惰受到的惩罚不仅仅是自己的失败,还有别人的成功。
🎅**学习目标: 坚持每一次的学习打卡

文章目录

    • 实现功能
    • 小黑记事本代码模板
      • 1.添加功能和列表渲染
      • 2.删除功能
      • 3.统计任务和清空任务
    • 案例总结

今天的学习任务是做一个小黑记事本,一个小案例进行练习。
经过前几周的学习,相信大家对vue基础知识已经有了大概的了解,复习之前学的知识一起做一个综合案例进行巩固练习吧!

如下是今天要完成的案例:
在这里插入图片描述

实现功能

如图所示,我们要做的是一个小黑记事本,完成功能有:

  1. 添加任务
  2. 删除任务
  3. 统计任务
  4. 清空任务

小黑记事本代码模板

将模板代码复制到编辑器中就可以使用了

<!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/index.css" />
<title>记事本</title>
</head>
<body><!-- 主体区域 -->
<section id="app"><!-- 输入框 --><header class="header"><h1>小黑记事本</h1><input placeholder="请输入任务" class="new-todo" /><button class="add">添加任务</button></header><!-- 列表区域 --><section class="main"><ul class="todo-list"><li class="todo"><div class="view"><span class="index">1.</span> <label>吃饭饭</label><button class="destroy"></button></div></li></ul></section><!-- 统计和清空 --><footer class="footer"><!-- 统计 --><span class="todo-count">合 计:<strong> 1 </strong></span><!-- 清空 --><button class="clear-completed">清空任务</button></footer>
</section><!-- 底部 -->
<script src="https://cdn.jsdelivr.net/npm/vue@2.7.14/dist/vue.js"></script>   
<script>const app = new Vue({el: '#app',data: {}})</script>
</body>
</html>

千万不要忘记引用vue,不然会报错

1.添加功能和列表渲染

添加功能实现步骤:
1.首先通过v-model进行绑定输入框获取表单元素输入内容
2.在点击添加任务的按钮上使用@click进行新增
3.在数组最前面添加内容 使用unshift
4.先将数据进行列表渲染

<!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/index.css" />
<title>记事本</title>
</head>
<body><!-- 主体区域 -->
<section id="app"><!-- 输入框 --><header class="header"><h1>小黑记事本</h1><input v-model="todoList" placeholder="请输入任务" class="new-todo" /><button class="add" @click="add">添加任务</button></header><!-- 列表区域 --><section class="main"><ul class="todo-list"><li class="todo" v-for="(item,index) in list" :key="item.id"><div class="view"><span class="index">{{index+1}}</span> <label>{{item.name}}</label><button class="destroy" @click="del(item.id)"></button></div></li></ul></section><!-- 统计和清空 --><footer class="footer"><!-- 统计 --><span class="todo-count">合 计:<strong> 1 </strong></span><!-- 清空 --><button class="clear-completed" >清空任务</button></footer>
</section><!-- 底部 -->
<script src="../vue.js"></script>
<script>const app = new Vue({el: '#app',data: {todoList:'',//渲染数据list:[{id:1,name:'跑步一小时'},{id:2,name:'跳舞一小时'},{id:3,name:'跳绳1000个'}]},methods:{//添加功能add(){输入框中内容不为空if(this.todoList.trim()===''){alert('请输入任务名称')return}this.list.unshift({id:+new Date(),name:this.todoList})this.todoList=''}}})</script>
</body>
</html>

1.首先我们要想将数据进行渲染,

2.我在添加任务中写了个警示框要先输入才可以进行添加数据
在这里插入图片描述3.可以随意写一些任务看是否可以进行添加
在这里插入图片描述

2.删除功能

<!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/index.css" />
<title>记事本</title>
</head>
<body><!-- 主体区域 -->
<section id="app"><!-- 输入框 --><header class="header"><h1>小黑记事本</h1><input v-model="todoList" placeholder="请输入任务" class="new-todo" /><button class="add" @click="add">添加任务</button></header><!-- 列表区域 --><section class="main"><ul class="todo-list"><li class="todo" v-for="(item,index) in list" :key="item.id"><div class="view"><span class="index">{{index+1}}</span> <label>{{item.name}}</label><button class="destroy" @click="del(item.id)"></button></div></li></ul></section><!-- 统计和清空 --><footer class="footer"><!-- 统计 --><span class="todo-count">合 计:<strong> 1 </strong></span><!-- 清空 --><button class="clear-completed" >清空任务</button></footer>
</section><!-- 底部 -->
<script src="../vue.js"></script>
<script>const app = new Vue({el: '#app',data: {todoList:'',//渲染数据;使用v-for渲染list:[{id:1,name:'跑步一小时'},{id:2,name:'跳舞一小时'},{id:3,name:'跳绳1000个'}]},methods:{//添加功能add(){if(this.todoList.trim()===''){alert('请输入任务名称')return}this.list.unshift({id:+new Date(),name:this.todoList})this.todoList=''},//删除功能del(id){this.list=this.list.filter(item=>item.id!==id)}}})</script>
</body>
</html>

在这里插入图片描述

3.统计任务和清空任务

<!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/index.css" />
<title>记事本</title>
</head>
<body><!-- 主体区域 -->
<section id="app"><!-- 输入框 --><header class="header"><h1>小黑记事本</h1><input v-model="todoList" placeholder="请输入任务" class="new-todo" /><button class="add" @click="add">添加任务</button></header><!-- 列表区域 --><section class="main"><ul class="todo-list"><li class="todo" v-for="(item,index) in list" :key="item.id"><div class="view"><span class="index">{{index+1}}</span> <label>{{item.name}}</label><button class="destroy" @click="del(item.id)"></button></div></li></ul></section><!-- 统计和清空  使用v-show隐藏空白部分 --><footer class="footer" v-show="list.length>0"><!-- 统计使用length计算list长度--><span class="todo-count">合 计:<strong>{{list.length}} </strong></span><!-- 清空 --><button class="clear-completed" @click="clear" >清空任务</button></footer>
</section><!-- 底部 -->
<script src="../vue.js"></script>
<script>const app = new Vue({el: '#app',data: {todoList:'',//渲染数据list:[{id:1,name:'跑步一小时'},{id:2,name:'跳舞一小时'},{id:3,name:'跳绳1000个'}]},methods:{//添加功能add(){if(this.todoList.trim()===''){alert('请输入任务名称')return}this.list.unshift({id:+new Date(),name:this.todoList})this.todoList=''},//删除功能del(id){this.list=this.list.filter(item=>item.id!==id)},clear(){this.list=[]}}})</script>
</body>
</html>

如图所示
1.计算任务总数
在这里插入图片描述
2,清空所有的任务
在这里插入图片描述

案例总结

总结:
1.列表渲染:
v-for :key设置 {{}}插值语法
2.删除功能:
v-on调用传参filter过滤 覆盖修改原数组
3.添加功能;
v-model绑定 unshift修改原数组添加
4.底部统计和清空
数组.length累计长度
unshift覆盖数组清空列表
v-show 控制隐藏

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

相关文章:

  • vite4+vue3+electron23.3+ts桌面应用bs端开发 打包windows、linux、max三个系统的安装包
  • 限制 el-input 输入 emoji
  • 【AI】解决Number_Words的安装和使用
  • 开启MySQL的binlog日志
  • 【支付宝小程序】支付宝小程序自定义组件技术教程
  • CSDN编程题-每日一练(2023-08-23)
  • 解决:Appium Inspector刷新页面一直加载转圈
  • Apache Doris 入门教程34:Join 优化
  • 【神州数码】BGP路由器案例
  • gin框架实现大文件下载
  • 数据可视化-canvas-svg-Echarts
  • 深信服 SG上网优化管理系统 catjs.php 任意文件读取漏洞[2023-HW]
  • java反序列化泛型中json对象
  • Docker Compose一键管理容器
  • API接口文档利器:Swagger 和 接口调试利器:Postman
  • Redis手动实现分布式锁-Demo
  • BBS项目day04 文章详情页、点赞点菜、评论功能(根评论和子评论)、评论分页之刷新评论页面
  • 【带着学Pytorch】1、pytorch的安装与入门
  • smartbi token回调获取登录凭证漏洞
  • SQL注入之堆叠查询
  • java-JVM 类加载机制
  • 前端面试:【网络协议与性能优化】提升Web应用性能的策略
  • 前端面试:【React】构建现代Web的利器
  • 使用mysql:5.6和 owncloud 镜像,构建一个个人网盘。
  • k8s发布应用
  • 微信小程序教学系列(4)
  • Netty核心源码解析(三)--NioEventLoop
  • Vue2学习笔记のVue核心
  • 把matlab的m文件打包成单独的可执行文件
  • redis 6个节点(3主3从),始终一个节点不能启动