vuecli练习之添加一个todo操作

涉及到三个 组件

 

header

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

<script>
import { nanoid } from "nanoid";
export default {
    name:"MyHeader",
    methods: {
        add(e){
            console.log(this)
            //如果输入项为空则停止执行
            if (!e.target.value.trim())  return  alert("输入不能为空")
            // if (!this.title.trim())  return  alert("输入不能为空")
              
           

            // 将用户的输入包装成一个todo对象
            const todoObj={id:nanoid(),title:e.target.value,done:false}
            // console.log(todoObj)

            this.addTodo(todoObj)//添加加工后的todoObj

            e.target.value=""//清空输入
        }
    },
    props:[
        "addTodo"//将加工好的函数传过来
    ]
}
</script>

<style>
    /*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>

app

<template>
    <div id="app">
      <div class="todo-container">
        <div class="todo-wrap">
          
          <MyHeader :addTodo="addTodo"></MyHeader>

          <MyList :todos="todos"></MyList>
          
          <MyFooter></MyFooter>
        </div>
      </div>
    </div>
</template>

<script>
import MyFooter from './components/MyFooter.vue'
import MyHeader from './components/MyHeader.vue'
// import MyItem from './components/MyItem.vue' item为list的子组件
import MyList from './components/MyList.vue'
// import School from './components/School.vue'
export default {
  name: 'app',
  components: {
    MyHeader,
    MyFooter,
    
    // MyItem,
    MyList
    // School
  },
  data() {
        return {
            todos:[
                {id:"001",title:"吃饭",done:true},
                {id:"002",title:"抽烟",done:false},
                {id:"003",title:"烫头",done:true},
            ]
        }
    },
  methods: {
    addTodo(todoObj){//添加一个toido
      this.todos.unshift(todoObj)
    }
  },
}
</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-edit {
  color: #fff;
  background-color: lightgreen;
  border: 1px solid green;
  margin-right: 5px;
}

.btn-danger:hover {
  color: #fff;
  background-color: #bd362f;
}

.btn-edit:hover {
  color: #fff;
  background-color: green;
}

.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>

list

<template>
      <ul class="todo-main">
        <MyItem v-for="(todoObj) in todos" :key="todoObj.id" :todo="todoObj"></MyItem>
    </ul>
</template>

<script>
import  MyItem  from "./MyItem";
export default {
    name:"MyList",
    components:{MyItem},
    /* data() {
        return {
            todos:[
                {id:"001",title:"吃饭",done:true},
                {id:"002",title:"抽烟",done:false},
                {id:"003",title:"烫头",done:true},
            ]
        }
    }, */
    props:[
        "todos"
    ]
}
</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>

 

posted @ 2022-11-10 14:00  小白字太白  阅读(41)  评论(0编辑  收藏  举报