vue实现一个添加删除操作

index.html

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1.0">
    <title>y</title>
  </head>
  <body>
    <div id="app"></div>
    <!-- built files will be auto injected -->
  </body>
</html>

main.js

// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'
import './base.css'

Vue.config.productionTip = false

/* eslint-disable no-new */
new Vue({
  el: '#app',
  components: { App },
  template: '<App/>',
})

App.vue

<template>
  <div id="app">
    <div class="todo-container">
      <div class="todo-wrap">
        <!-- <TodoHeader @addTodo="addTodo" /> -->
        <TodoHeader ref="header" />
        <todo-list :todos="todos" />

        <TodoFooter>
          <input type="checkbox" v-model="isAllCheck" slot="checkAll" />
          <span slot="count">已完成{{completeSize}} / 全部{{todos.length}}</span>
          <button class="btn btn-dander" v-show="completeSize" @click="deleteComleteTodos" slot="delete">清除已完成任务</button>
        </TodoFooter>
      </div>
    </div>
  </div>
</template>

<script>
  import TodoHeader from './components/TodoHeader.vue'
  import TodoList from './components/TodoList.vue'
  import TodoFooter from './components/TodoFooter.vue'
  import PubSub from 'pubsub-js'
  import storageUtil from './util/storageUtil'

  export default {
    data() {
      return {
        todos: storageUtil.readTodos()
      }
    },

    computed: {
      completeSize() {
        return this.todos.reduce((preTotal, todo) => preTotal + (todo.complete ? 1 : 0), 0)
      },

      isAllCheck: {
        get() {
          return this.completeSize === this.todos.length && this.completeSize
        },

        set(value) {
          this.selectAllTodos(value)
        }
      }
    },

    mounted() {
      this.$refs.header.$on('addTodo', this.addTodo)

      PubSub.subscribe('deleteTodo', (msg, index) => {
        this.deleteTodo(index)
      })
    },

    methods: {
      addTodo(todo) {
        this.todos.unshift(todo)
      },

      deleteTodo(index) {
        this.todos.splice(index, 1)
      },

      deleteComleteTodos() {
        this.todos = todos.filter(todo => !todo.complete)
      },

      selectAllTodos(check) {
        this.todos.forEach(todo => todo.complete = check)
      }
    },

    watch: { //深度监视
      todos: {
        deep: true, //深度监视
        /* handler: function(value) {
          //window.localStorage.setItem('todos_key', JSON.stringify(value))
          storageUtil.saveTodos(value)
        } */
        handler:storageUtil.saveTodos
      }
    },

    components: {
      TodoHeader,
      TodoList,
      TodoFooter
    }

  }
</script>

<style>
  .todo-container {
    width: 600px;
    margin: 0 auto;
  }

  .todo-container .todo-wrap {
    padding: 10px;
    border: 1px solid #ddd;
    border-radius: 5px;
  }
</style>

base.css

body {
  background: #FFFFFF;
}

.btn-danger {
  color: #fff;
  background-color:#da4f49;
  border: 1px solid #bd362f;
}

.btn-dander:hover {
  color: #FFF;
  background-color: #bd362f;
}

.btn:focus {
  outline: none;
}

TodoHeader.vue

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

<script>
  export default {
    props: {

    },
    data() {
      return {
        title: ''
      }
    },

    methods: {
      addItem() {
        //检查输入的合法性
        const title = this.title.trim();
        if (!title) {
          alert('必须输入')
          return
        }
        //根据输入生成一个todo对象
        const todo = {
          title,
          complete: false
        }
        //添加到todos
        //this.addTodo(todo)
        //触发自定义事件
        this.$emit('addTodo', todo)
        //清除输入
        this.title = ''
      }
    }
  }
</script>

<style>
  .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.8);
  }
</style>

TodoList.vue

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

<script>
  export default {
    props: {

    },
    data() {
      return {
        title: ''
      }
    },

    methods: {
      addItem() {
        //检查输入的合法性
        const title = this.title.trim();
        if (!title) {
          alert('必须输入')
          return
        }
        //根据输入生成一个todo对象
        const todo = {
          title,
          complete: false
        }
        //添加到todos
        //this.addTodo(todo)
        //触发自定义事件
        this.$emit('addTodo', todo)
        //清除输入
        this.title = ''
      }
    }
  }
</script>

<style>
  .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.8);
  }
</style>

TodoItem.vue

<template>
  <li @mouseenter="hadnleEnter(true)" @mouseleave="hadnleEnter(false)" :style="{background: bgColor}">
    <label>
      <input type="checkbox" v-model="todo.complete">
      <span>{{todo.title}}</span>
    </label>
    <button class="btn btn-dander" v-show="isShow" @click="deleteItem">删除</button>
  </li>
</template>

<script>
  import PubSub from 'pubsub-js'
  export default {
    props: {
      todo: Object,
      index: Number
    },

    data() {
      return {
        bgColor: 'white', //默认背景颜色
        isShow: false //默认是否显示
      }
    },

    methods: {
      hadnleEnter(isEnter) {
        if (isEnter) {
          this.bgColor = '#aaaaaa'
          this.isShow = true
        } else {
          this.bgColor = 'white'
          this.isShow = false
        }
      },

      deleteItem() {
        const {
          todo,
          index,
          deleteTodo
        } = this
        if (window.confirm(`确认删除${todo.title}吗?`)) {
          //deleteTodo(index)
          PubSub.publish('deleteTodo', index)
        }
      }
    }
  }
</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;
    margin-top: 3px;
    padding: 6px 12px;
    vertical-align: middle;
    border: 1px solid transparent;
    border-radius: 4px;
    color: white;
  }

  li:before {
    content: initial;
  }

  li:last-child {
    border-radius: none;
  }

  li .btn-dander {
    background: red;
  }
</style>

TodoFooter.vue

<template>
  <div class="todo-footer">
    <label>
      <!-- <input type="checkbox" v-model="isAllCheck"/> -->
      <slot name="checkAll"></slot>
    </label>
    <span>
      <!-- <span>已完成{{completeSize}}</span>/ 全部{{todos.length}} -->
      <slot name="count"></slot>
    </span>
    <!-- <button class="btn btn-dander" v-show="completeSize" @click="deleteComleteTodos">清除已完成任务</button> -->
    <slot name="delete"></slot>
  </div>
</template>

<script>
  export default {

  }
</script>

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

  button {
    float: right;
    margin-top: 3px;
    padding: 6px 12px;
    vertical-align: middle;
    border: 1px solid transparent;
    border-radius: 4px;
    color: white;
    background: red;
  }
</style>

storageUtil.js

/* 使用locastorage存储数据工具
1.函数
2.对象
需要向外暴露一个功能还是多个功能
*/
const TODOS_KEY = 'todos_key'
export default {
  saveTodos (todos) {
     window.localStorage.setItem(TODOS_KEY, JSON.stringify(todos))
  },

  readTodos() {
    return JSON.parse(window.localStorage.getItem(TODOS_KEY) || '[]')
  }
}

目录结构

在这里插入图片描述

效果图

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值