Vue之TodoList案例详解

2022-04-15 0 527

Vue之TodoList案例详解

Vue之TodoList案例详解

<template>
  <div id="root">
    <div class="todo-container">
      <div class="todo-wrap">
        <Top :received="received" />
        <List :todos="todos" :checkTodo="checkTodo" :deleteTodo="deleteTodo" />
        <Bottom :todos="todos" :checkAllTodo="checkAllTodo" :clearAllTodo="clearAllTodo" />
      </div>
    </div>
  </div>
</template>
<script>
  import Top from './components/Top.vue'
  import Bottom from './components/Bottom.vue'
  import List from './components/List.vue'
  export default {
    name: 'App',
    components: {
      Top,
      List,
      Bottom
    },
    data() {
      return {
        todos: [{
            id: '001',
            title: '吃饭',
            done: true
          },
          {
            id: '002',
            title: '睡觉',
            done: false
          },
          {
            id: '003',
            title: '打豆豆',
            done: false
          },
        ]
      }
    },
    methods: {
      //添加一个todo
      received(todoObj) {
        this.todos.unshift(todoObj);
      },
      //取消勾选todo
      checkTodo(id) {
        this.todos.forEach((todo) => {
          //函数体
          if (todo.id === id) todo.done = !todo.done;
        })
      },
      //删除
      deleteTodo(id) {
        this.todos = this.todos.filter(todo => todo.id !== id)
      },
      //全选 全不选
      checkAllTodo(done) {
        this.todos.forEach((todo) => {
          todo.done = done
        })
      },
      //清除所有已经完成的数据
      clearAllTodo() {
        this.todos = this.todos.filter((todo) => {
          return !todo.done
        })
      }
    }
  }
</script>
<style lang="css">
  /*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>

总结

本篇文章就到这里了,希望能够给你带来帮助,也希望您能够多多关注NICE源码的更多内容!

免责声明:
1、本网站所有发布的源码、软件和资料均为收集各大资源网站整理而来;仅限用于学习和研究目的,您必须在下载后的24个小时之内,从您的电脑中彻底删除上述内容。 不得使用于非法商业用途,不得违反国家法律。否则后果自负!

2、本站信息来自网络,版权争议与本站无关。一切关于该资源商业行为与www.niceym.com无关。
如果您喜欢该程序,请支持正版源码、软件,购买注册,得到更好的正版服务。
如有侵犯你版权的,请邮件与我们联系处理(邮箱:skknet@qq.com),本站将立即改正。

NICE源码网 JavaScript Vue之TodoList案例详解 https://www.niceym.com/20054.html