vue3實(shí)現(xiàn)一個(gè)todo-list
更新時(shí)間:2021年08月24日 11:41:13 作者:YuShiYue
這篇文章主要為大家詳細(xì)介紹了基于vuejs實(shí)現(xiàn)一個(gè)todolist項(xiàng)目,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能給你帶來(lái)幫助
實(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組件編寫之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)文章
關(guān)于springboot配置druid數(shù)據(jù)源不生效問(wèn)題(踩坑記)
今天日常跟著網(wǎng)課學(xué)習(xí),學(xué)到了整合druid數(shù)據(jù)源,遇到了好幾個(gè)坑,希望這篇文章可以幫助一些和我一樣踩坑的人2021-09-09
Spring Boot整合SSE實(shí)時(shí)通信的問(wèn)題小結(jié)
本文介紹了服務(wù)器發(fā)送事件(Server-Sent Events,SSE)技術(shù),其主要特點(diǎn)包括單向數(shù)據(jù)流、自動(dòng)重連、自定義事件類型等,SSE適用于實(shí)時(shí)更新場(chǎng)景,如新聞推送、評(píng)論系統(tǒng)等,感興趣的朋友跟隨小編一起看看吧2025-01-01
自己動(dòng)手在Spring-Boot上加強(qiáng)國(guó)際化功能的示例
這篇文章主要介紹了自己動(dòng)手在Spring-Boot上加強(qiáng)國(guó)際化功能的示例,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-04-04

