赞
踩
前言:todolist案例真的算是经典了,不管你是学习原生js,还是学习jq,还是学习vue,亦或者是react,todolist是练习必不可少的练习demo了,下面我们来看看这个案例。需要完整代码的小朋友戳这里获取资料https://download.csdn.net/download/TroyeSivanlp/60226570
首先,创建项目骨架。
module.exports = {
devServer: { // 自定义服务配置
open: true,
port: 3000
},
lintOnSave: false // 关闭eslint检查
}
<template> <div class="todoapp"> <TodoHeader :arr="list" @create="createFn"></TodoHeader> <TodoMain :arr="showArr" @del="deleteFn"></TodoMain> <TodoFooter :farr="showArr" @changeType="typeFn" @clear="clearFun" ></TodoFooter> </div> </template> <script> // 1. 目标: 创建工程-准备相关组件文件-标签+样式 (静态) // 1.0 样式引入 import "./styles/base.css" import "./styles/index.css" // 1.1 组件引入 import TodoHeader from './components/TodoHeader' import TodoMain from './components/TodoMain' import TodoFooter from './components/TodoFooter' export default { // 1.2 组件注册 components: { TodoHeader, TodoMain, TodoFooter }, // 2. 目标: 铺设待办任务 // 2.0 - 准备数据 data(){ return { // 8.1 默认从本地取值 list: JSON.parse(localStorage.getItem('todoList')) || [], // 6.4 先中转接收类型字符串 getSel: "all" // 默认显示全部 } }, methods: { createFn(taskName){ // 添加任务 // 3.3 push到数组里 let id = this.list.length == 0 ? 100 : this.list[this.list.length - 1].id + 1 this.list.push({ id: id, name: taskName, isDone: false }) }, deleteFn(theId){ // 删除任务 let index = this.list.findIndex(obj => obj.id === theId) this.list.splice(index, 1) }, typeFn(str){ // 'all' 'yes' 'no' // 修改类型 this.getSel = str }, clearFun(){ // 清除已完成 this.list = this.list.filter(obj => obj.isDone == false) } }, // 6.5 定义showArr数组 - 通过list配合条件筛选而来 computed: { showArr(){ if (this.getSel === 'yes') { // 显示已完成 return this.list.filter(obj => obj.isDone === true) } else if (this.getSel === 'no') { // 显示未完成 return this.list.filter(obj => obj.isDone === false) } else { return this.list // 全部显示 } } }, // 8. 目标: 数据缓存 watch: { list: { deep: true, handler(){ // 8.0 只要list变化 - 覆盖式保存到localStorage里 localStorage.setItem('todoList', JSON.stringify(this.list)) } } } } </script> <style> </style>
<template> <header class="header"> <h1>todos</h1> <!-- 9. 目标: 全选状态 9.0 v-model关联全选状态 页面变化(勾选true, 未勾选false) -> v-model -> isAll变量 --> <input id="toggle-all" class="toggle-all" type="checkbox" v-model="isAll" /> <label for="toggle-all"></label> <!-- 3.0 键盘事件-回车按键 3.1 输入框 - v-model获取值 --> <input class="new-todo" placeholder="输入任务名称-回车确认" autofocus @keydown.enter="downFn" v-model="task" /> </header> </template> <script> // 3. 目标 - 新增任务 export default { data() { return { task: "", }; }, methods: { downFn() { if (this.task.trim().length === 0) { alert("任务名不能为空"); return; } // 3.2(重要) - 当前任务名字要加到list数组里 // 子传父技术 this.$emit("create", this.task); this.task = ""; }, }, // 9.1 定义计算属性 computed: { isAll: { set(checked) { // 只有true / false // 9.3 影响数组里每个小选框绑定的isDone属性 this.arr.forEach((obj) => (obj.isDone = checked)); }, get() { // 9.4 小选框统计状态 -> 全选框 // 9.5 如果没有数据, 直接返回false-不要让全选勾选状态 return ( this.arr.length !== 0 && this.arr.every((obj) => obj.isDone === true) ); }, }, }, // 9.2 父 -> 子 list数组 props: ["arr"], }; </script>
<template> <ul class="todo-list"> <!-- 2.2 循环任务-关联选中状态-铺设数据 --> <!-- completed: 完成的类名 --> <li :class="{completed: obj.isDone}" v-for="obj in arr" :key='obj.id'> <div class="view"> <input class="toggle" type="checkbox" v-model="obj.isDone"/> <label>{{ obj.name }}</label> <!-- 4.0 注册点击事件 --> <button class="destroy" @click="delFn(obj.id)"></button> </div> </li> </ul> </template> <script> // 4. 目标: 删除list里数据 export default { // 2.1 定义props props: ['arr'], methods: { delFn(id){ // 4.1 子传父 this.$emit('del', id) } } } </script>
<template> <footer class="footer"> <span class="todo-count">剩余<strong>{{ count }}</strong></span> <ul class="filters" @click="fn"> <li> <!-- 6.1 判断谁应该有高亮样式: 动态class 6.2 用户点击要切换isSel里保存的值 --> <a :class="{selected: isSel === 'all'}" href="javascript:;" @click="isSel='all'">全部</a> </li> <li> <a :class="{selected: isSel === 'no'}" href="javascript:;" @click="isSel='no'">未完成</a> </li> <li> <a :class="{selected: isSel === 'yes'}" href="javascript:;" @click="isSel='yes'">已完成</a> </li> </ul> <!-- 7. 目标: 清除已完成 --> <!-- 7.0 点击事件 --> <button class="clear-completed" @click="clearFn">清除已完成</button> </footer> </template> <script> // 5. 目标: 数量统计 export default { // 5.0 props定义 props: ['farr'], // 5.1 计算属性 - 任务数量 computed: { count(){ return this.farr.length } }, // 6. 目标: 点谁谁亮 // 6.0 变量isSel data(){ return { isSel: 'all' // 全部:'all', 已完成'yes', 未完成'no' } }, methods: { fn(){ // 切换筛选条件 // 6.3 子 -> 父 把类型字符串传给App.vue this.$emit("changeType", this.isSel) }, clearFn(){ // 清空已完成任务 // 7.1 触发App.vue里事件对应clearFun方法 this.$emit('clear') } } } </script>
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。