1.组件化编码流程:
(1).拆分静态组件:组件要按照功能点拆分,命名不要与html元素冲突。
(2).实现动态组件:考虑好数据的存放位置,数据是一个组件在用,还是一些组件在用:

  1).一个组件在用:放在组件自身即可。
  2).一些组件在用:放在他们共同的父组件上(状态提升)。(3).实现交互:从绑定事件开始。
  1. props适用于:
    (1).父组件==>子组件通信
    (2).子组件==>父组件通信(要求父先给子一个函数)
    3.使用v-model时要切记: v-model绑定的值不能是props传过来的值,因为props是不可以修改的!
    4.props传过来的若是对象类型的值,修改对象中的属性时Vue不会报错,但不推荐这样做。

APP.VUE

<template>
  <div id="app">
    <div id="root">
        <div class="todo-container">
          <div class="todo-wrap">
              <!-- 头部搜索框组件 -->
              <headerModule :receive="receive"/>
              <listModule :todos="todos" :checkTodo="checkTodo" :deleteTodo="deleteTodo"></listModule>
              <footerModule :todos="todos" :checkAllTodo="checkAllTodo" :clearAllTodo="clearAllTodo"></footerModule>
          </div>
        </div>
    </div> 
    
  </div>
</template>

<script>
  // import的名称就是组件的name
  import headerModule from './components/Header.vue'
  import listModule from './components/List.vue'
  import footerModule from './components/Footer.vue'
  export default {
    name: 'App',
    data(){
            return{
                todos:[
                    {id:'001',title:'吃饭',done:true},
                    {id:'002',title:'睡觉',done:false},
                    {id:'003',title:'打游戏',done:true},
                    {id:'004',title:'上课',done:false},
                ]
            }
        },
    components: { 
      headerModule,
      listModule,
      footerModule
    },
    methods:{
      // 添加数据
      receive(x){
        this.todos.unshift(x)
      },
      // 改变复选框
      checkTodo(id){
        this.todos.forEach((todo)=>{
          if(todo.id==id){
            todo.done=!todo.done
          } 
        })
      },
      // 删除数据
      deleteTodo(id){
        this.todos=this.todos.filter(
          function(todo){
            return todo.id !==id
          }
        )
      },
      // 全选按钮
      checkAllTodo(done){
        this.todos.forEach((todo)=>{
          todo.done=done
        })
      },
      clearAllTodo(){
        this.todos=this.todos.filter((todo)=>{
          return !todo.done
        })
      }
    }


  }

</script>

<style>
#app {
  font-family: Avenir, Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  color: #2c3e50;
  margin-top: 60px;
}
/*分割线 */
/*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>

Header.VUE

<template>
    <div class="todo-header">
        <input v-model="title" @keyup.enter="add" type="text" placeholder="请输入你的任务名称,按回车键确认"/>
    </div>
</template>

<script>
    import {nanoid} from 'nanoid'
export default {
    name:'headerModule',
    data(){
        return{
            title:''
        }
    },
    methods:{
        add(e){
            if(!this.title.trim())return
            const todoObj={
                id:nanoid(),
                title:this.title,
                done:false
            }
            this.receive(todoObj)
            this.title=''
        }
    },
    props:['receive'],

}
</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">
        <itemModule 
        v-for="todoObj in todos" 
        :key=todoObj.id 
        :todo="todoObj" 
        :checkTodo="checkTodo"
        :deleteTodo="deleteTodo"
        >
        </itemModule>
    </ul>
      
  
</template>

<script>
    import itemModule from './Item.vue'
    export default {
        name:'listModule',
        components:{
            itemModule
        },
        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>
            <input type="checkbox" :checked='todo.done' @change="gaibian(todo.id)"/>
            <span>{{todo.title}}</span>
        </label>
        <button class="btn btn-danger" @click="del(todo.id)" >删除</button>
    </li>
</template>

<script>
export default {
    name:'itemModule',
    props:['todo','checkTodo','deleteTodo'],
    methods:{
        gaibian(id){
            // 通知app改变复选框状态
            this.checkTodo(id)
        },
        del(id){
            if(confirm('确定要删除信息吗?')){
                // 通知app删除数组数据
                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:hover{
        background-color: #ddd;
    }
    li:hover button{
        display: block;
    }

    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;
    }

</style>

Footer.Vue

<template>
    <div class="todo-footer" v-show="todos.length"><!--如果length有长度就是为真,例如123456都是真,如果长度为0那就是假,v-show是靠真假来显示隐藏的,绝了。-->
        <label>
          <input type="checkbox" :checked='isAll' @change="checkAll"/>
        </label>
        <span>
          <span>已完成{{doneTotal}}</span> / 全部{{todos.length}}
        </span>
        <button class="btn btn-danger" @click="clearAll">清除已完成任务</button>
      </div>
</template>

<script>
export default {
  props:['todos','checkAllTodo','clearAllTodo'],
  name:'footerModule',
  computed:{
    doneTotal(){
      return this.todos.filter(todo=>todo.done).length//用filter将done值为ture的筛选出来复制给一个新的数组,然后再取这个数组的length长度。
    },
    isAll(){
      return this.doneTotal===this.todos.length && this.todos.length>0
    }
  },
  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>

xPj7VI.png