TodoList案例目录

main.js
import Vue from 'vue'
import App from './App.vue'
Vue.config.productionTip = false;
new Vue({
render: h => h(App),
beforeCreate() {
Vue.prototype.$bus = this
}
}).$mount('#app')
App.vue
<template>
<div class="todo-container">
<div class="todo-wrap">
<!-- 向子组件传递一个函数 -->
<SearchHeader @addItem="addItem" />
<!--使用全局事件总线,就可以不用这些:delItem="delItem"
:changeStatus="changeStatus" -->
<SearchList :dataList="doList" />
<SearchFooter
:doList="doList"
@checkedStatus="checkedStatus"
@delDone="delDone"
/>
</div>
</div>
</template>
<script>
import SearchHeader from "./components/SearchHeader.vue";
import SearchFooter from "./components/SearchFooter.vue";
import SearchList from "./components/SearchList.vue";
import pubsub from "pubsub-js";
export default {
components: {
SearchHeader,
SearchFooter,
SearchList,
},
data() {
return {
doList: JSON.parse(localStorage.getItem("doList")) || [],
pubId: 0,
};
},
methods: {
addItem(x) {
this.doList.unshift(x);
},
changeStatus(id) {
this.doList.forEach((item) => {
if (item.id == id) {
item.done = !item.done;
}
});
},
delItem(_, v) {
this.doList = this.doList.filter((i) => {
return i.id != v;
});
},
checkedStatus(done) {
this.doList.forEach((v) => {
v.done = done;
});
},
delDone() {
this.doList = this.doList.filter((v) => {
return !v.done;
});
},
updateTitle(id, title) {
this.doList.forEach((item) => {
if (item.id == id) {
item.title = title;
}
});
},
},
watch: {
doList: {
deep: true,
handler(value) {
localStorage.setItem("doList", JSON.stringify(value));
},
},
},
mounted() {
this.$bus.$on("changeStatus", this.changeStatus);
this.pubId = pubsub.subscribe("delItem", this.delItem);
this.$bus.$on("updateItem", this.updateTitle);
},
beforeDestroy() {
this.$bus.$off(["changeStatus"]);
pubsub.unsubscribe(this.pubId);
},
};
</script>
<style >
body {
background: #fff;
}
.btn {
display: inline-block;
padding: 4px 12px;
margin-bottom: 0;
font-size: 14px;
line-height: 20px;
text-align: center;
vertical-align: middle;
cursor: pointer;
box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.2), 0px, 1px, 2px,
rgba(0, 0, 0, 0.05);
border-radius: 4px;
}
.btn-danger {
color: #fff;
background-color: #da4f49;
border: 1px solid #bd362f;
}
.btn-edit {
background: green;
border: 1px solid green;
color: #fff;
margin-right: 10px;
}
.btn-danger:hover {
color: #fff;
background-color: #bd362f;
}
.btn:focus {
outline: none;
}
.todo-container {
width: 600px;
margin: 0 auto;
overflow: hidden;
}
.todo-container .todo-wrap {
padding: 10px;
border: 1px solid #ddd;
border-radius: 5px;
}
</style>>
SearchHeader.vue
<template>
<div class="todo-header">
<input
type="text"
placeholder="请输入你的任务名称,按回车键确认"
@keyup.enter="addItems"
v-model="title"
/>
<span v-if="flag" class="showtips">*内容不能为空</span>
</div>
</template>
<script>
import { nanoid } from "nanoid";
export default {
name: "SearchHeader",
data() {
return {
title: "",
flag: false,
};
},
methods: {
addItems() {
if (!this.title.trim()) {
this.flag = true;
this.title = "";
return;
}
this.flag = false;
const obj = {
id: nanoid(),
title: this.title.trim(),
done: false,
};
this.$emit("addItem", obj);
this.title = "";
},
},
};
</script>
<style scoped>
.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), 0px, 0x, 8px,
rgba(82, 168, 236, 0.6);
}
.showtips {
font-size: 10px;
color: red;
}
</style>
SearchList.vue
<template>
<ul class="todo-main">
<transition-group appear name="item">
<!-- 通过props属性,父子组件传值,将item传递给子组件 -->
<SearchItem v-for="item in dataList" :key="item.id" :items="item" />
<!-- :changeStatus="changeStatus"
:delItem="delItem" -->
</transition-group>
</ul>
</template>
<script>
import SearchItem from "./SearchItem.vue";
export default {
name: "SearchList",
components: {
SearchItem,
},
props: ["dataList"],
};
</script>
<style scoped>
.todo-main {
margin-left: 0;
border: 1px solid #ddd;
border-radius: 5px;
padding-left: 0;
margin-top: 10px;
overflow: hidden;
}
.todo-empty {
height: 40px;
line-height: 40px;
border: 1px solid #ddd;
border-radius: 2px;
padding-left: 5px;
margin-top: 10px;
}
.item-enter-active {
animation: showTime 1s linear;
}
.item-leave-active {
animation: showTime 1s linear reverse;
}
@keyframes showTime {
from {
transform: translateX(100%);
}
to {
transform: translateX(0px);
}
}
</style>
SearchItem.vue
<template>
<!-- <transition name="item" appear> -->
<li>
<label for="">
<!--方式一: @click="handler(items.id)" -->
<input
type="checkbox"
:checked="items.done"
@change="handler(items.id)"
/>
<!-- 以下代码也能实现修改状态的功能,但是不建议用,因为有点违反原则,因为修改了props -->
<!-- <input type="checkbox" v-model="items.done" /> -->
<span v-show="!items.isEdit">{{ items.title }}</span>
<!-- blur失去焦点时触发 -->
<input
ref="inpTitle"
type="text"
:value="items.title"
v-show="items.isEdit"
@keyup.enter="handleEdit(items, $event)"
/>
</label>
<button class="btn btn-danger" @click="handleDel(items.id)">删除</button>
<button
class="btn btn-edit"
@click="showEdit(items)"
v-show="!items.isEdit"
>
编辑
</button>
</li>
<!-- </transition> -->
</template>
<script>
import pubsub from "pubsub-js";
export default {
name: "SearchItem",
data() {
return {};
},
props: ["items"],
methods: {
handler(id) {
this.$bus.$emit("changeStatus", id);
},
handleDel(id) {
if (confirm("确定删除该任务吗?")) {
pubsub.publish("delItem", id);
} else {
return;
}
},
showEdit(item) {
if (Object.prototype.hasOwnProperty.call(item, "isEdit")) {
item.isEdit = true;
} else {
this.$set(item, "isEdit", true);
}
this.$nextTick(function () {
this.$refs.inpTitle.focus();
});
},
handleEdit(item, e) {
item.isEdit = false;
if (!e.target.value.trim()) {
alert("任务名不能为空");
return;
}
this.$bus.$emit("updateItem", item.id, e.target.value);
},
},
};
</script>
<style scoped>
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 input {
vertical-align: middle;
margin-right: 6px;
position: relative;
top: -1px;
}
li button {
float: right;
display: none;
margin-top: 3px;
}
li:hover {
background: #ddd;
}
li:hover button {
display: block;
}
li:before {
content: initials;
}
li:last-child {
border-bottom: none;
}
</style>
SearchFooter.vue
<template>
<div class="todo-footer">
<label>
<!-- 这个做法就需要用到dom元素 -->
<!-- <input type="checkbox" :checked="isAll" @change="checkAll" /> -->
<!-- checkbox的value值就是:true/false,双向绑定的做法用不到dom元素 -->
<input type="checkbox" v-model="isAll" />
</label>
<span>已完成{{ doneTotal }} / 全部 {{ total }}</span>
<button class="btn btn-danger" @click="handleDone">清除已完成任务</button>
</div>
</template>
<script>
export default {
name: "SearchFooter",
props: ["doList"],
data() {
return {};
},
methods: {
handleDone() {
this.$emit("delDone");
},
},
computed: {
doneTotal() {
return this.doList.reduce((pre, item) => {
return pre + (item.done ? 1 : 0);
}, 0);
},
total() {
return this.doList.length;
},
isAll: {
get() {
return this.doneTotal == this.total && this.total && this.total > 0;
},
set(value) {
this.$emit("checkedStatus", value);
},
},
},
};
</script>
<style scoped>
.todo-footer {
height: 40px;
line-height: 40px;
padding-left: 6px;
margin-top: 5px;
}
.todo-footer label {
display: inline-block;
margin-right: 28px;
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>