前言
todoList是常见的手撸代码题了。通过vue3的composition api实现todoList,掌握setup
、ref
、reactive
、watch
、toRefs
、toRef
等钩子函数,快速上手vue3+ts技术栈。建议有vue2基础的边看官方文档边做,以便快速熟悉相关hook,最后的实现效果图如下:
下面直接上代码。
Index页面
App.vue
<template>
<div class="todo-container">
<h1>TodoList</h1>
<div class="todo-wrap">
<Header :addTodo="addTodo" />
<List :todos="todos" :deleteTodo="deleteTodo" :updateTodo="updateTodo" />
<Footer
:todos="todos"
:checkAll="checkAll"
:clearAllCompletedTodos="clearAllCompletedTodos"
/>
</div>
</div>
</template>
<script lang="ts">
import { defineComponent, onMounted, reactive, toRefs, watch } from "vue"
// 引入直接的子级组件
import Header from "@/components/todoList/Header.vue"
import List from "@/components/todoList/List.vue"
import Footer from "@/components/todoList/Footer.vue"
// 引入接口
import { Todo } from "@/types/todo"
// 数据持久化
import { saveTodos, readTodos } from "@/utils/localStorageUtils"
export default defineComponent({
name: "App",
// 注册组件
components: {
Header,
List,
Footer,
},
// 数据应该用数组来存储,数组中的每个数据都是一个对象,对象中应该有三个属性(id,title,isCompleted)
// 把数组暂且定义在App.vue父级组件
setup() {
// 定义一个数组数据
// const state = reactive<{ todos: Todo[] }>({
// todos: [
// { id: 1, title: "奔驰", isCompleted: false },
// { id: 2, title: "宝马", isCompleted: true },
// { id: 3, title: "奥迪", isCompleted: false },
// ],
// })
console.log("Demo11 setup")
// console.log(state)
const state = reactive<{ todos: Todo[] }>({
todos: [],
})
// 界面加载完毕后过了一会再读取数据
onMounted(() => {
setTimeout(() => {
state.todos = readTodos()
}, 1000)
})
// 添加数据的方法
const addTodo = (todo: Todo) => {
state.todos.unshift(todo)
}
// 删除数据的方法
const deleteTodo = (index: number) => {
state.todos.splice(index, 1)
}
// 修改todo的isCompleted属性的状态
const updateTodo = (todo: Todo, isCompleted: boolean) => {
todo.isCompleted = isCompleted
}
// 全选或者是全不选的方法
const checkAll = (isCompleted: boolean) => {
state.todos.forEach(todo => {
todo.isCompleted = isCompleted
})
}
// 清理所有选中的数据
const clearAllCompletedTodos = () => {
state.todos = state.todos.filter(todo => !todo.isCompleted)
}
// 监视操作:如果todos数组的数据变化了,直接存储到浏览器的缓存中 有多种写法
// watch(()=>state.todos,(value)=>{
// // 保存到浏览器的缓存中
// localStorage.setItem('todos_key',JSON.stringify(value))
// },{deep:true})
// watch(
// () => state.todos,
// (value) => {
// // 保存到浏览器的缓存中
// saveTodos(value)
// },
// { deep: true }
// )
watch(() => state.todos, saveTodos, { deep: true })
return {
...toRefs(state),
addTodo,
deleteTodo,
updateTodo,
checkAll,
clearAllCompletedTodos,
}
},
})
</script>
<style scoped>
/*app*/
.todo-container {
width: 600px;
margin: 0 auto;
}
.todo-container .todo-wrap {
padding: 10px;
border: 1px solid #ddd;
border-radius: 5px;
}
</style>
工具函数
主要用于数据持久化到localStorage中,这里也可存到cookies中。
utils/localStorageUtils.ts
import { Todo } from '@/types/todo'
// 保存数据到浏览器的缓存中
export function saveTodos(todos: Todo[]) {
localStorage.setItem('todos_key', JSON.stringify(todos))
}
// 从浏览器的缓存中读取数据
export function readTodos(): Todo[] {
return JSON.parse(localStorage.getItem('todos_key') || '[]')
}
Item组件
components/todoList/Item.vue
<template>
<li
@mouseenter="mouseHandler(true)"
@mouseleave="mouseHandler(false)"
:style="{ backgroundColor: bgColor, color: myColor }"
>
<label>
<input type="checkbox" v-model="isComptete" />
<span>{{ todo.title }}</span>
</label>
<button class="btn btn-danger" v-show="isShow" @click="delTodo">
删除
</button>
</li>
</template>
<script lang="ts">
import { defineComponent, ref, computed } from "vue"
// 引入接口
import { Todo } from "@/types/todo"
export default defineComponent({
name: "Item",
props: {
todo: {
type: Object as () => Todo, // 函数返回的是Todo类型
required: true,
},
deleteTodo: {
type: Function,
required: true,
},
index: {
type: Number,
required: true,
},
updateTodo: {
type: Function,
required: true,
},
},
setup(props: any) {
const todo: Todo = props.todo
// 背景色
const bgColor = ref("white")
// 前景色
const myColor = ref("black")
// 设置按钮默认不显示
const isShow = ref(false)
// 鼠标进入和离开事件的回调函数
const mouseHandler = (flag: boolean) => {
if (flag) {
// 鼠标进入
bgColor.value = "pink"
myColor.value = "green"
isShow.value = true
} else {
// 鼠标离开
bgColor.value = "white"
myColor.value = "black"
isShow.value = false
}
}
// 删除数据的方法
const delTodo = () => {
// 提示
if (window.confirm("确定要删除吗?")) {
props.deleteTodo(props.index)
}
}
// 计算属性的方式---来让当前的复选框选中/不选中
const isComptete = computed({
get() {
return todo.isCompleted
},
set(val) {
props.updateTodo(todo, val)
},
})
return {
mouseHandler,
bgColor,
myColor,
isShow,
delTodo,
isComptete,
}
},
})
</script>
<style scoped>
/*item*/
li {
list-style: none;
height: 36px;
line-height: 36px;
padding: 0 5px;
border-bottom: 1px solid #ddd;
}
li label {
float: left;
cursor: pointer;
}
li label li input {
vertical-align: middle;
margin-right: 6px;
position: relative;
top: -1px;
}
li button {
float: right;
/* display: none; */
margin-top: 3px;
}
li:before {
content: initial;
}
li:last-child {
border-bottom: none;
}
</style>
List组件
components/todoList/List.vue
<template>
<ul class="todo-main">
<Item
v-for="(todo, index) in todos"
:key="todo.id"
:todo="todo"
:deleteTodo="deleteTodo"
:updateTodo="updateTodo"
:index="index"
/>
</ul>
</template>
<script lang="ts">
import { defineComponent } from "vue"
// 引入子级组件
import Item from "./Item.vue"
export default defineComponent({
name: "List",
components: {
Item,
},
props: ["todos", "deleteTodo", "updateTodo"],
setup(props: any, context: any) {
console.log(props)
console.log(context)
},
})
</script>
<style scoped>
/*main*/
.todo-main {
margin-left: 0px;
border: 1px solid #ddd;
border-radius: 2px;
padding: 0px;
}
.todo-empty {
height: 40px;
line-height: 40px;
border: 1px solid #ddd;
border-radius: 2px;
padding-left: 5px;
margin-top: 10px;
}
</style>
Header组件
components/todoList/Header.vue
<template>
<div class="todo-header">
<input
type="text"
placeholder="请输入你的任务名称,按回车键确认"
v-model="title"
@keyup.enter="add"
/>
</div>
</template>
<script lang="ts">
import { defineComponent, ref } from "vue"
import { Todo } from "@/types/todo"
// 定义接口,约束对象的类型
export default defineComponent({
name: "Header",
props: {
addTodo: {
type: Function,
required: true, // 必须
},
},
setup(props: any) {
// 定义一个ref类型的数据
const title = ref("")
// 回车的事件的回调函数,用来添加数据
const add = () => {
// 获取文本框中输入的数据,判断不为空
const text = title.value
if (!text.trim()) return
// 此时有数据,创建一个todo对象
const todo = {
id: Date.now(),
title: text,
isCompleted: false,
}
// 调用方法addTodo的方法
props.addTodo(todo)
console.log("添加todo", todo)
// 清空文本框
title.value = ""
}
return {
title,
add,
}
},
})
</script>
<style scoped>
/*header*/
.todo-header input {
width: 560px;
height: 28px;
font-size: 14px;
border: 1px solid #ccc;
border-radius: 4px;
padding: 4px 7px;
}
.todo-header input:focus {
outline: none;
border-color: rgba(82, 168, 236, 0.8);
box-shadow: inset 0 1px 1px rgba(0, 0, 0, 0.075),
0 0 8px rgba(82, 168, 236, 0.6);
}
</style>
components/todoList/Footer.vue
<template>
<div class="todo-footer">
<label>
<input type="checkbox" v-model="isCheckAll" />
</label>
<span>
<span>已完成{{ count }}</span> / 全部{{ todos.length }}
</span>
<button class="btn btn-danger" @click="clearAllCompletedTodos">
清除已完成任务
</button>
</div>
</template>
<script lang="ts">
import { defineComponent, computed } from "vue"
import { Todo } from "@/types/todo"
export default defineComponent({
name: "Footer",
props: {
todos: {
type: Array as () => Todo[],
required: true,
default: [],
},
checkAll: {
type: Function,
required: true,
},
clearAllCompletedTodos: {
type: Function,
required: true,
},
},
setup(props: any) {
console.log("Footer setup")
console.log(props.todos)
// 已完成的计算属性操作
const count = computed(() => {
console.log("Footer computed todos")
console.log(props.todos)
return props.todos.reduce(
(pre, todo, index) => pre + (todo.isCompleted ? 1 : 0),
0
)
})
// 全选/全不选的计算属性操作
const isCheckAll = computed({
get() {
return count.value > 0 && props.todos.length === count.value
},
set(val) {
props.checkAll(val)
},
})
return {
count,
isCheckAll,
}
},
})
</script>
<style scoped>
/*footer*/
.todo-footer {
height: 40px;
line-height: 40px;
padding-left: 6px;
margin-top: 5px;
}
.todo-footer label {
display: inline-block;
margin-right: 20px;
cursor: pointer;
}
.todo-footer label input {
position: relative;
top: -1px;
vertical-align: middle;
margin-right: 5px;
}
.todo-footer button {
float: right;
margin-top: 5px;
}
</style>
原文始发于微信公众号(豆子前端):[vue3]vue3+typescript上手案例:实现todoList
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/56611.html