项目描述
项目实现功能有:记录今天要完成的任务,勾选已经完成的任务,删除已经完成的全部任务。
界面展示:
代码展示
创建一个Myitem.vue文件夹
<template>
<li>
<label >
<input type="checkbox" :checked="todo.done" @click="handle(todo.id)"/>
<span>{{todo.title}}</span>
</label>
<button class="btn btn-danger" @click="delect(todo.id)" >删除</button>
</li>
</template>
<script>
export default {
name:'MyItem',
props:['todo','checkTodo','delectTodo'],
methods:{
handle(id){
//通知app去将tudo.down取反
this.checkTodo(id)
},
delect(id){
//通知app删除当前todoObj
this.delectTodo(id)
}
}
}
</script>
<style scoped>
li{
list-style:none;
height:36px;
line-height: 36px;
padding:0 50px;
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>
2.在components文件夹下创建 Myheader文件夹
<template>
<div class="todo-header">
<input type="text"
@keydown.enter="add"
placeholder="请输入你的任务名称,按回车键确认"
v-model="value"/>
</div>
</template>
<script>
//引入
import {nanoid} from 'nanoid'
export default {
name:'MyHeader',
data(){
return {
value:''}
},
props:['addtodos'],
methods:{
add(e){
//校验数据
if(!this.value.trim()){
return alert('输入不能为空!')
}
const todoObj={id:nanoid(),title:this.value,done:false}
this.addtodos(todoObj)
this.value=''
}
},
}
</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),0 0 8px rgba(82,168,236,0.6);
}
</style>
3.在同一个文件夹下创建MyFooter.vue文件夹
<template>
<div class = 'todo-footer' v-if="todosAll">
<label for="">
<input type="checkbox" :checked="isAll" @click="allTodo"/>
</label>
<span>已完成{{countDone}}/全部{{todosAll}}</span>
<button class="btn btn-danger" @click="delectDoneall">清除已经完成的内容</button>
</div>
</template>
<script>
export default {
name:'MyFooter',
props:['todos','delectDone'],
date(){
},
methods:{
allTodo(e){
if(e.target.checked==true){
this.todos.forEach((todo)=>{
todo.done=true
})
}
else{
this.todos.forEach((todo)=>{
todo.done=false
})
}
},
delectDoneall(){
this.delectDone()
}
},
computed:{
todosAll(){
return this.todos.length
},
countDone(){
let i =0
this.todos.forEach(todo => {
if(todo.done==true){
i++
}}
)
return i
},
isAll(){
return this.countDone === this.todosAll && this.todosAll >0
},
}
}
</script>
<style scoped>
.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>
4.在同个文件夹下创建MyList.vue文件
<template>
<ul class="todo-main">
<MyItem
v-for="todoObj in todos"
:key="todoObj.id"
:todo="todoObj"
:checkTodo="checkTodo"
:delectTodo="delectTodo">
</MyItem>
</ul>
</template>
<script>
import MyItem from './MyItem.vue'
export default {
name:'MyList',
components:{
MyItem,
},
props:['todos','checkTodo','delectTodo']
}
</script>
<style scoped>
.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>
5.再创建app.vue文章来源:https://www.toymoban.com/news/detail-472530.html
<template>
<div id="app">
<div class="todo-container">
<div class="todo-wrap">
<!--放置静态资源-->
<MyHeader :addtodos="addtodos"></MyHeader>
<MyList
:todos="todos"
:checkTodo="checkTodo"
:delectTodo="delectTodo">
</MyList>
<MyFooter
:todos="todos"
:delectDone="delectDone" >
</MyFooter>
</div>
</div>
</div>
</template>
<script>
//所有组件放置的文件夹,除了app.vue
import MyHeader from './components/MyHeader.vue'
import MyList from'./components/MyList.vue'
import MyFooter from './components/MyFooter.vue'
export default {
name: 'App',
components: {
MyFooter,
MyHeader,
MyList
},
data(){
return{
todos:[
{id:'001',title:'写代码',done:true},
{id:'002',title:'看书',done:false},
{id:'003',title:'休息',done:true}
],
i:0,
n:0
}
},
methods:{
//添加一个todo
addtodos(todoObj){
this.todos.unshift(todoObj)
},
//勾选或者取消勾选一个todo
checkTodo(id){
this.todos.forEach((todo)=>{
if(todo.id ==id){
todo.done=!todo.done
}
})
},
//删除对应的todo
delectTodo(id){
this.todos=this.todos.filter((todo)=>{
console.log('运行到了这里')
return todo.id !==id
})
console.log(this.todo.id,id)
},
//清除已经完成的内容
delectDone(){
this.todos=this.todos.filter((todo)=>{
return todo.done === false
})
}
},
}
</script>
<style>
/*basic*/
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 o rgba(225,225,225,0.2) , 0 1px 2px rgba(0,0, 0, 0.2);
border-radius: 4px;
}
.btn-danger{
color:#fff;
background-color:#da4f49;
border:1px solid #bd362f;
}
.btn-danger:hover{
color:#fff;
background-color:#bd362f ;
}
.btn:foucs{
outline:none;
}
.todo-container{
width:600px;
margin:0 auto;
}
.todo-container .todo-wrap{
padding:10px;
border:1px solid #ddd;
border-radius: 5px;
}
</style>
总结
1.组件化编码流程:
(1)拆分静态组件:组件要按照功能点拆分,命名不要与html元素冲突
(2)实现动态组件:考虑好数据的存放位置,数据是一个组件在用还是一些组件在用:
一个组件在用的话放在组件本身
一些组件在用的话放在对应的组件的父组件上
(3)实现交互:从绑定事件开始文章来源地址https://www.toymoban.com/news/detail-472530.html
2.props适合用于:
(1)父组件向子组件通信
(2)子组件向父组件通信
3.使用v-model的时候要记得:v-model绑定的数据的值不能是props中传递过来的数据,因为props不能修改
4,props传递的值如果是对象类型,那么改变对象中的属性的话,不会报错,但是不建议这么做。
5.获取框是否选中用e.target.checked (type=‘checked’),获取文本框的文字用e.target.value
6.可以使用过滤器来删除数组中不符合条件的组员
语法:arrs = arrs.filter((arr)={return 条件})
到了这里,关于Vue设计记事本的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!