VUE3.0,DAY49
Todo-list案例实现静态组件
使用vue创建脚手架,在components页面下新建四个自定义组件如下所示,自定义组件的名字不要和H5内置的标签起冲突,比如起名叫MyHeader。
组件内的基础代码如下所示,注意修改name与文件名相对应,四个组件内代码类似进行修改。
//MyHeader.vue内的代码
<template>
<div></div>
</template>
<script>
export default {
name: "MyHeader",
};
</script>
<style>
</style>
然后在App.vue引入组件,不需要引入Item.vue,因为List.vue已经包括了Item.vue在内,即Item.vue是子,List.vue是父,调用List.vue就等于也调用了Item.vue。然后进行注册引入的组件。
注意:现在的App.vue和List.vue内的代码进行了变动:
//APP.VUE代码
<template>
<div>
<School></School>
<hr />
<Student></Student>
<hr />
</div>
</template>
<script>
//引入自定义的组件
import MyHeader from './components/MyHeader.vue'
import MyFooter from './components/MyFooter.vue'
import List from './components/List.vue'
//注册组件
export default {
name: "App",
components: { MyHeader,MyFooter, List},
};
</script>
<style>
</style>
//List.vue代码
<template>
<div></div>
</template>
<script>
// 因为List包含Item,所以这里引入Item
import Item from "./Item.vue";
export default {
name: "List",
components: { Item },
};
</script>
<style>
</style>
最终静态模式下,各个文件内的代码如下,增添了template页面的结构,和style样式
//App.vue
<template>
<div id="root">
<div class="todo-container">
<div class="todo-wrap">
<MyHeader :addTodo="addTodo"/>
<List :todos="todos" :checkTodo="checkTodo" :deleteTodo="deleteTodo"/>
<MyFooter :todos="todos" :checkAllTodo="checkAllTodo" :clearAllTodo="clearAllTodo"/>
</div>
</div>
</div>
</template>
<script>
//引入自定义的组件
import MyHeader from './components/MyHeader.vue'
import MyFooter from './components/MyFooter.vue'
import List from './components/List.vue'
//注册组件
export default {
name: "App",
components: { MyHeader,MyFooter, List},
};
</script>
<style>
/*base*/
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), 0 1px 2px rgba(0, 0, 0, 0.05);
border-radius: 4px;
}
.btn-danger {
color: #fff;
background-color: #da4f49;
border: 1px solid #bd362f;
}
.btn-danger:hover {
color: #fff;
background-color: #bd362f;
}
.btn:focus {
outline: none;
}
.todo-container {
width: 600px;
margin: 0 auto;
}
.todo-container .todo-wrap {
padding: 10px;
border: 1px solid #ddd;
border-radius: 5px;
}
</style>
//item.vue代码
<template>
<li>
<label>
<input
type="checkbox"
:checked="todo.done"
@change="handleCheck(todo.id)"
/>
<!-- 如下代码也能实现功能,但是不太推荐,因为有点违反原则,因为修改了props -->
<!-- <input type="checkbox" v-model="todo.done"/> -->
<span>{{ todo.title }}</span>
</label>
<button class="btn btn-danger" @click="handleDelete(todo.id)">删除</button>
</li>
</template>
<script scoped>
export default {
name: "Item",
};
</script>
<style>
/*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;
}
li:hover {
background-color: #ddd;
}
li:hover button {
display: block;
}
</style>
//List.vue代码
<template>
<ul class="todo-main">
<Item
v-for="todoObj in todos"
:key="todoObj.id"
:todo="todoObj"
:checkTodo="checkTodo"
:deleteTodo="deleteTodo"
/>
</ul>
</template>
<script scoped>
// 因为List包含Item,所以这里引入Item
import Item from "./Item.vue";
export default {
name: "List",
components: {Item},
};
</script>
<style>
/*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>
//Myfooter.vue代码
<template>
<div class="todo-footer" v-show="total">
<label>
<!-- <input type="checkbox" :checked="isAll" @change="checkAll"/> -->
<input type="checkbox" v-model="isAll"/>
</label>
<span>
<span>已完成{{doneTotal}}</span> / 全部{{total}}
</span>
<button class="btn btn-danger" @click="clearAll">清除已完成任务</button>
</div>
</template>
<script>
export default {
name: "MyFooter",
};
</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>
//myheader.vue代码
<template>
<div class="todo-header">
<input type="text" placeholder="请输入你的任务名称,按回车键确认" v-model="title" @keyup.enter="add"/>
</div>
</template>
<script>
export default {
name: "MyHeader",
};
</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>
案例实现各项功能
实现输入框按回车键添加事情,勾选事情和取消勾选,删除全部事情等等。使用vs code打开新终端然后运行服务即可。
App.vue代码
<template>
<div id="root">
<div class="todo-container">
<div class="todo-wrap">
<MyHeader :addTodo="addTodo" />
<!-- 因为平级的兄弟组件间不能直接传递参数,所以把header中输入的要做的事todos先传到App.vue父组件中,
然后再由父组件传入子组件List中进行更新展示 -->
<List :todos="todos" :checkTodo="checkTodo" :deleteTodo="deleteTodo" />
<MyFooter
:todos="todos"
:checkAllTodo="checkAllTodo"
:clearAllTodo="clearAllTodo"
/>
</div>
</div>
</div>
</template>
<script>
//引入自定义的组件
import MyHeader from "./components/MyHeader.vue";
import List from "./components/List.vue";
import MyFooter from "./components/MyFooter.vue";
//注册组件
export default {
name: "App",
components: { MyHeader, List, MyFooter },
data() {
return {
// 在列表中想做的事,都写在todo数组里,标上ID,名字,完成的标志
todos: [
{ id: "001", title: "吃饭", done: "true" },
{ id: "002", title: "睡觉", done: "false" },
{ id: "003", title: "喝酒", done: "true" },
],
};
},
methods: {
//添加一个todoObj
addTodo(todoObj) {
this.todos.unshift(todoObj);
},
//取消勾选一个todoObj
checkTodo(id) {
//forEach是遍历的意思
this.todos.forEach((todo) => {
//如果我们接受到的取消勾选后todo的id和todos里的id相同,我们就取反done的值
if (todo.id === id) todo.done = !todo.done;
});
},
//删除一个todo
deleteTodo(id) {
this.todos = this.todos.filter((todo) => todo.id !== id);
},
//全选or取消全选
checkAllTodo(done) {
this.todos.forEach((todo) => {
todo.done = done;
});
},
//清除所有已经完成的todo
clearAllTodo() {
this.todos = this.todos.filter((todo) => {
return !todo.done;
});
},
},
};
</script>
<style>
/*base*/
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),
0 1px 2px rgba(0, 0, 0, 0.05);
border-radius: 4px;
}
.btn-danger {
color: #fff;
background-color: #da4f49;
border: 1px solid #bd362f;
}
.btn-danger:hover {
color: #fff;
background-color: #bd362f;
}
.btn:focus {
outline: none;
}
.todo-container {
width: 600px;
margin: 0 auto;
}
.todo-container .todo-wrap {
padding: 10px;
border: 1px solid #ddd;
border-radius: 5px;
}
</style>
myheader.vue
<template>
<div class="todo-header">
<!-- 给一个按键响应,keyup.enter -->
<input
type="text"
placeholder="请输入你的任务名称,按回车键确认"
v-model="title"
@keyup.enter="add"
/>
</div>
</template>
<script>
//引入nanoid
import { nanoid } from "nanoid";
export default {
name: "MyHeader",
//接收从App传递过来的addTodo
props: ["addTodo"],
data() {
return {
//配置一个title空的,以便双向绑定按键添加事件的内容
title: "",
};
},
methods: {
add(e) {
//校验输入的数据,如果输入为空,就弹窗
if (!this.title.trim()) return alert("输入不能为空");
//将用户的输入包装成一个todo对象
//使用uuid的精简版nanoid库,来生成全球唯一的id
const todoObj = { id: nanoid(), title: e.target.value, done: false };
//通知App组件去添加一个todo对象
this.addTodo(todoObj);
//list拿到输入的数据后,头部输入框清空输入
this.title = "";
},
},
};
</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>
list.vue
<template>
<ul class="todo-main">
<!-- v-for是遍历的意思,todoObj是遍历到的东西,todos是一堆要做的事 -->
<!-- 传参数,传id,传要做的事,传是否做完,传删除该事 -->
<Item
v-for="todoObj in todos"
:key="todoObj.id"
:todo="todoObj"
:checkTodo="checkTodo"
:deleteTodo="deleteTodo"
/>
</ul>
</template>
<script scoped>
// 因为List包含Item,所以这里引入Item
import Item from "./Item.vue";
export default {
name: "List",
components: { Item },
//接受通过父组件传过来要做的事todos,checkTodo和deleteTodo是给子组件MyItem用的
props: ["todos", "checkTodo", "deleteTodo"],
};
</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>
item.vue
<template>
<li>
<label>
<!-- :checked="true"的意思是是否想给这个input标签checked选项打勾,如果是true,就勾选,那么我们这里的true或者false是由List文件中的todo.done决定的 -->
<input
type="checkbox"
:checked="todo.done"
@change="handleCheck(todo.id)"
/>
<!-- 如下代码也能实现功能,但是不太推荐,因为有点违反原则,因为修改了props -->
<!-- <input type="checkbox" v-model="todo.done"/> -->
<span>{{ todo.title }}</span>
</label>
<button class="btn btn-danger" @click="handleDelete(todo.id)">删除</button>
</li>
</template>
<script scoped>
export default {
name: "Item",
//声明接收todo、checkTodo、deleteTodo
props: ["todo", "checkTodo", "deleteTodo"],
methods: {
//勾选or取消勾选
handleCheck(id) {
//通知App组件对应的todo对象的done值取反
this.checkTodo(id);
},
//删除
handleDelete(id) {
if (confirm("确定删除吗?")) {
//通知App组件将对应的todo对象删除
this.deleteTodo(id);
}
},
},
};
</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;
}
li:hover {
background-color: #ddd;
}
li:hover button {
display: block;
}
</style>
myfooter.vue
<template>
<div class="todo-footer" v-show="total">
<label>
<!-- <input type="checkbox" :checked="isAll" @change="checkAll"/> -->
<input type="checkbox" v-model="isAll" />
</label>
<span>
<span>已完成{{ doneTotal }}</span> / 全部{{ total }}
</span>
<button class="btn btn-danger" @click="clearAll">清除已完成任务</button>
</div>
</template>
<script>
export default {
name: "MyFooter",
props: ["todos", "checkAllTodo", "clearAllTodo"],
computed: {
//总数
total() {
return this.todos.length;
},
//已完成数
doneTotal() {
//此处使用reduce方法做条件统计
/* const x = this.todos.reduce((pre,current)=>{
console.log('@',pre,current)
return pre + (current.done ? 1 : 0)
},0) */
//简写
return this.todos.reduce((pre, todo) => pre + (todo.done ? 1 : 0), 0);
},
//控制全选框
isAll: {
//全选框是否勾选
get() {
return this.doneTotal === this.total && this.total > 0;
},
//isAll被修改时set被调用
set(value) {
this.checkAllTodo(value);
},
},
},
methods: {
/* checkAll(e){
this.checkAllTodo(e.target.checked)
} */
//清空所有已完成
clearAll() {
this.clearAllTodo();
},
},
};
</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>
最后的输出效果如下:
总结
1.组件化编码流程:
(1).拆分静态组件:组件要按照功能点拆分,命名不要与html元素冲突。
(2).实现动态组件:考虑好数据的存放位置,数据是一个组件在用,还是一些组件 在用:
1).一个组件在用的东西 : 放在组件自身即可。
2).一些(多个)组件在用的东西 : 放在他们共同的父组件上(<span style=" color:red" >状态提升</span>
)
(3).实现交互:从绑定事件开始。
2. props适用于:
(1).父组件==>子组件通信
(2).子组件==>父组件通信(要求父先给子一个函数)
3.使用v-modelI时要切记: v-model绑定的值不能是props传过来的值,因为props确认的值是
不可以修改的
4. props传过来的若是对象类型的值,修改对象中的属性时Vue不会报错,但不推
荐这样做。