vue3實(shí)現(xiàn)一個(gè)todo-list
實(shí)現(xiàn)方式不是最優(yōu),主要是為了學(xué)習(xí)vue3的一些新語(yǔ)法以及屬性
功能介紹



項(xiàng)目的搭建可以參考這篇文章
vue3.x+vite+element-ui+vue-router+vuex+axios搭建項(xiàng)目
相關(guān)代碼
index.vue
<template>
<div class="todo-list">
<el-card class="box-card">
<template #header>
<div class="card-header">
<span>工作計(jì)劃</span>
<el-button
class="button"
type="primary"
icon="el-icon-circle-plus"
circle
@click="handleClickTodo"
></el-button>
</div>
</template>
<template v-if="list.length > 0">
<todo-item
v-for="(val, index) in list"
:key="index"
:info="val"
></todo-item>
</template>
<el-empty v-else description="還沒(méi)有待辦的事項(xiàng)~"></el-empty>
</el-card>
<add-action v-model:visible="visible"></add-action>
</div>
</template>
<script>
import AddAction from "./AddTodo.vue";
import TodoItem from "./Item.vue";
import { reactive, toRefs, provide } from "vue";
export default {
name: "todo-list",
components: { AddAction, TodoItem },
setup() {
const state = reactive({
visible: false,
list: [
{
title: "1.學(xué)習(xí)vue3.0",
time: "2021-08-20",
desc: "1.ppppppppppppp",
status: false,
},
],
});
const addTodo = (data) => {
state.list.push(data);
};
const delTodo = (title) => {
state.list = state.list.filter((val) => val.title !== title);
};
const handleClickTodo = () => {
state.visible = true;
};
provide("addTodo", addTodo);
provide("delTodo", delTodo);
return {
handleClickTodo,
...toRefs(state),
};
},
};
</script>
<style>
.todo-list {
padding: 100px;
}
.card-header {
display: flex;
justify-content: space-between;
align-items: center;
}
.text {
font-size: 14px;
}
.item {
margin-bottom: 18px;
}
.box-card {
width: 480px;
}
</style>
AddTodo.vue
<template>
<el-dialog
title="新增待辦計(jì)劃"
v-model="visible"
width="600px"
@close="handleClose"
>
<el-form
style="width: 430px"
:model="form"
:rules="rules"
label-width="120px"
ref="formRef"
>
<el-form-item label="計(jì)劃名稱" prop="title">
<el-input
v-model="form.title"
placeholder="請(qǐng)輸入待辦計(jì)劃名稱"
></el-input>
</el-form-item>
<el-form-item label="計(jì)劃完成時(shí)間" prop="time">
<el-date-picker value-format="YYYY/MM/DD" style="width: 100%" v-model="form.time" type="date" placeholder="請(qǐng)選擇計(jì)劃完成時(shí)間">
</el-date-picker>
</el-form-item>
<el-form-item label="計(jì)劃詳細(xì)描述" prop="desc">
<el-input
type="textarea"
:rows="6"
v-model="form.desc"
placeholder="請(qǐng)輸入詳細(xì)待辦計(jì)劃"
></el-input>
</el-form-item>
</el-form>
<template #footer>
<span class="dialog-footer">
<el-button @click="visible = false">取 消</el-button>
<el-button type="primary" @click="handleConfirm">確 定</el-button>
</span>
</template>
</el-dialog>
</template>
<script>
import { reactive, toRefs, ref, inject } from "vue";
export default {
name: "add-todo",
props: {
visible: {
type: Boolean,
default: false,
},
},
setup(props, { emit }) {
const formRef = ref(null)
const addTodo = inject('addTodo')
const state = reactive({
form: {
title: "",
desc: "",
time: "",
status: false
},
rules: {
title: [
{ required: true, message: '請(qǐng)輸入待辦計(jì)劃名稱', trigger: ['blur']}
],
time: [
{ required: true, message: '請(qǐng)選擇待辦計(jì)劃時(shí)間', trigger: ['change']}
],
desc: [
{ required: true, message: '請(qǐng)輸入詳細(xì)待辦計(jì)劃', trigger: ['blur']}
]
},
});
const handleClose = () => {
emit("update:visible", false);
formRef.value.clearValidate()
formRef.value.resetFields()
};
const handleConfirm = () => {
formRef.value.validate(valid => {
if (valid) {
addTodo(JSON.parse(JSON.stringify(state.form)))
handleClose()
}
})
}
return {
formRef,
...toRefs(state),
handleClose,
handleConfirm
};
},
};
</script>
Item.vue
<template>
<div class="todo-item">
<div class="titme-box">
<el-checkbox v-model="info.status" @click="change"></el-checkbox>
<h3 :class="info.status ? 'success' : ''">{{ info.title }}</h3>
</div>
<div class="del">
<p class="time" :class="info.status ? 'success' : ''">{{ info.time }}</p>
<el-button
type="danger"
icon="el-icon-delete"
circle
size="mini"
@click="handleDelTodo"
></el-button>
</div>
</div>
</template>
<script>
import { inject } from "vue";
export default {
name: "todo-item",
props: {
info: {
type: Object,
default: () => ({}),
},
},
setup(props) {
const delTodo = inject("delTodo");
const handleDelTodo = () => {
delTodo(props.info.title);
};
return {
handleDelTodo,
};
},
};
</script>
<style lang="scss">
.todo-item {
display: flex;
align-items: center;
justify-content: space-between;
border-bottom: 1px solid rgb(229, 226, 226);
height: 45px;
line-height: 45px;
.success {
text-decoration: line-through;
}
.titme-box {
display: flex;
align-items: center;
h3 {
padding-left: 12px;
font-size: 16px;
}
}
.del {
display: flex;
align-items: center;
.time {
width: 100px;
font-size: 14px;
}
}
}
</style>

總結(jié)
本篇文章就到這里了,希望能給你帶來(lái)幫助,也希望您能夠多多關(guān)注腳本之家的更多內(nèi)容!
- vue組件編寫(xiě)之todolist組件實(shí)例詳解
- 使用Vue完成一個(gè)簡(jiǎn)單的todolist的方法
- vue實(shí)現(xiàn)留言板todolist功能
- 詳解Vue的computed(計(jì)算屬性)使用實(shí)例之TodoList
- Vue.js實(shí)現(xiàn)一個(gè)todo-list的上移下移刪除功能
- 基于vuejs實(shí)現(xiàn)一個(gè)todolist項(xiàng)目
- vue實(shí)現(xiàn)todolist單頁(yè)面應(yīng)用
- vue實(shí)現(xiàn)ToDoList簡(jiǎn)單實(shí)例
- vue3組合式API實(shí)現(xiàn)todo列表效果
相關(guān)文章
java如何讀取properties文件將參數(shù)值配置到靜態(tài)變量
這篇文章主要介紹了java如何讀取properties文件將參數(shù)值配置到靜態(tài)變量問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-08-08
Spring?Boot?Admin集成與自定義監(jiān)控告警示例詳解
SpringBootAdmin是一個(gè)管理和監(jiān)控SpringBoot應(yīng)用程序的工具,可通過(guò)集成和配置實(shí)現(xiàn)應(yīng)用監(jiān)控與告警功能,本文給大家介紹Spring?Boot?Admin集成與自定義監(jiān)控告警示例詳解,感興趣的朋友跟隨小編一起看看吧2024-09-09
解決SpringBoot的@DeleteMapping注解的方法不被調(diào)用問(wèn)題
這篇文章主要介紹了解決SpringBoot的@DeleteMapping注解的方法不被調(diào)用問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-01-01
Java利用WatchService監(jiān)聽(tīng)文件變化示例
本篇文章主要介紹了Java利用WatchService監(jiān)聽(tīng)文件變化示例,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-10-10
Java 線程池_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理
系統(tǒng)啟動(dòng)一個(gè)新線程的成本是比較高的,因?yàn)樗婕暗脚c操作系統(tǒng)的交互。在這種情況下,使用線程池可以很好的提供性能,尤其是當(dāng)程序中需要?jiǎng)?chuàng)建大量生存期很短暫的線程時(shí),更應(yīng)該考慮使用線程池2017-05-05
java虛擬機(jī)運(yùn)行時(shí)數(shù)據(jù)區(qū)分析
這篇文章主要介紹了java虛擬機(jī)運(yùn)行時(shí)數(shù)據(jù)區(qū)分析,具有一定參考價(jià)值,需要的朋友可以了解下。2017-11-11

